首页 > 解决方案 > 如何突出显示回收站视图项目的选定和未选定文本和图标?

问题描述

在我的主要布局中,我有一个地图片段和一个 RecyclerView,如下所示:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/places_recycler"></android.support.v7.widget.RecyclerView>
</LinearLayout>

RecyclerView 包含带有图标(ImageView)、文本(TextView)和位置详细信息的项目。当一个项目添加图标和文本显示在 recyclerView 和它的位置详细信息显示在地图上。它工作正常。但我想要选定项目的突出显示图标,并更改地图上该项目标记的颜色,并将未选定项目的图标从突出显示更改为正常形式,并将其标记为默认颜色。
我的 RecyclerView 项目代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/places"
        android:layout_alignParentRight="true"
        android:id="@+id/places_locIc"
        />
    <CustomViews.CustomTextView
        android:layout_marginRight="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/places_locIc"
        android:id="@+id/places_address"/>
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:padding="7dp"
        android:src="@drawable/ic_edit"
        android:id="@+id/places_edit"/>
    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:padding="7dp"
        android:layout_below="@id/places_edit"
        android:id="@+id/places_remove"
        android:src="@drawable/remove"/>
</RelativeLayout>

RecyclerView 适配器:

package Adapters;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Marker;
import com.prj.shopt.R;
import com.prj.shopt.Utility;

import java.util.List;

import Models.Place;

public class PlaceRecyclerAdapter extends RecyclerView.Adapter<PlaceRecyclerAdapter.PlaceHolder> implements View.OnClickListener {
    List<Place> placeList;
    public List<Marker> markers;
    public PlaceRecyclerAdapter(List<Place> places,List<Marker> markers){
        this.placeList=places;
        this.markers=markers;
    }
    @Override
    public PlaceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.places_item,parent,false);
        return new PlaceHolder(view,markers,parent.getContext());
    }

    @Override
    public void onBindViewHolder(PlaceHolder holder, int position) {
        holder.bind(placeList.get(position));
    }

    @Override
    public int getItemCount() {
        return placeList.size();
    }

    @Override
    public void onClick(View view) {

    }

    static class PlaceHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        int preMarkerPosition;
        int preImageId;
        ImageView edit,delete;
        TextView address;
        List<Marker> markers;
        public PlaceHolder(View itemView, final List<Marker> markers, final Context context) {
            super(itemView);
            this.markers=markers;
            edit=itemView.findViewById(R.id.places_edit);
            delete=itemView.findViewById(R.id.places_remove);
            address=itemView.findViewById(R.id.places_address);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(preMarkerPosition<0) {
                        preMarkerPosition = 0;
                        //preImageId=view.findViewById(R.id.places_locIc).getId();
                    }
                    markers.get(preMarkerPosition).setIcon(BitmapDescriptorFactory.fromBitmap(Utility.smallMarker(context)));
                    markers.get(getLayoutPosition()).setIcon(BitmapDescriptorFactory.fromBitmap(Utility.bigMarker(context)));
                    (view.findViewById(R.id.places_locIc))
                            .setBackground(context.getResources().getDrawable(R.drawable.placehighlight));
                    //(view.findViewById(preImageId)).setBackground(context.getResources().getDrawable(R.drawable.placesdefault));
                    preMarkerPosition=getLayoutPosition();
                }
            });
        }
        public void bind(Place place){
            address.setText(place.getCity());
            edit.setOnClickListener(this);
            delete.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {

        }
    }
}

我怎样才能做到这一点?

标签: androidandroid-recyclerview

解决方案


推荐阅读