之前实现选中某一条目 然后需要把其他条目拿过来再循环遍历一下 感觉很麻烦 有没有简单点的呢
贴代码 先看 item 很简单 就是两个图片 底部是背景色
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_marginTop="7dp"
android:layout_marginBottom="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="62dp"
android:layout_height="62dp">
<ImageView
android:orientation="vertical"
android:id="@+id/ll_bg"
android:visibility="gone"
android:src="@drawable/circle_red_shape"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:layout_gravity="center"
android:layout_margin="2dp"
android:src="@drawable/zb_tx_1"
android:id="@+id/iv_item_icon"
android:layout_width="60dp"
android:layout_height="60dp" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>
然后我们看adapter里
public class SelectIconAdapter extends RecyclerView.Adapter<SelectIconAdapter.MyViewHolder> {
private Context context;
private int[] carList;
private boolean[]isSelect;
private SelectIconAdapter.ItemClickListener mItemClickListener ;
private int selectedPosition=-1024;
public interface ItemClickListener{
public void onItemClick(int position) ;
}
public void setOnItemClickListener(SelectIconAdapter.ItemClickListener itemClickListener){
this.mItemClickListener = itemClickListener ;
}
public SelectIconAdapter(Context context, int[] carList,boolean[] isSelect) {
this.context=context;
this.carList=carList;
this.isSelect=isSelect;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {
// View inflater = LayoutInflater.from(context).inflate(R.layout.item_dome,parent,false);
View inflater = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item_icon, parent,false);
MyViewHolder holder = new MyViewHolder(inflater);
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.imageView.setImageDrawable(context.getResources().getDrawable(carList[i]));
myViewHolder.itemView.setSelected(selectedPosition == i);
if (selectedPosition == i) {
myViewHolder.llBg.setVisibility(View.VISIBLE);
} else {
myViewHolder.llBg.setVisibility(View.GONE);
}
if (mItemClickListener != null){
// if(isSelect[i]){
// myViewHolder.llBg.setVisibility(View.VISIBLE);
// }
// else {
// myViewHolder.llBg.setVisibility(View.GONE);
// }
myViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 这里利用回调来给RecyclerView设置点击事件
notifyItemChanged(selectedPosition);//刷新以前item 回置状态
selectedPosition = i; //选择的position赋值给参数,
notifyItemChanged(selectedPosition);//刷新当前点击item
mItemClickListener.onItemClick(i);
}
});
}
}
@Override
public int getItemCount() {
return 24;
}
class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
ImageView llBg;
public MyViewHolder(View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.iv_item_icon);
llBg=itemView.findViewById(R.id.ll_bg);
}
}
这里我们重点看下 这三行
notifyItemChanged(selectedPosition);//刷新以前item 回置状态
selectedPosition = i; //选择的position赋值给参数,
notifyItemChanged(selectedPosition);//刷新当前点击item
如果需要逐一全部选中 把notifyItemChanged(selectedPosition); 这行注释即可
如果需要只选中一个 之前选中恢复状态 放开即可
在此谢谢 https://www.jianshu.com/p/088e93832268 是在他提供的思路上改进的