在我的主要布局中,我有一个地图片段和一个像这样的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)和位置详细信息的项目。当一个项目在recylcerView中添加了图标和文本时,它的位置详细信息显示在地图上。它工作正常。但是我希望突出显示所选项目的图标,并在地图上更改此项目的标记颜色,并将未选项目的图标从突出显示更改为普通形式,并将其标记为默认颜色。
我的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) {
}
}
}
我该怎么做?