recyclerview给item添加选中效果(多选)

大体实现思路:

使用相对布局,在recyclerview的item整体布局底层添加一个CheckBox,在adapter中引用一个Map标记选中CheckBox,然后在bandview中给CheckBox setOnCheckedChangeListener改变CheckBox背景、向Map中添加标记.

demo链接在文末.

具体实现:

1.activity:

  
RecyclerView recyclerview;
Adapter mAdapter;
StringBuffer buffer = new StringBuffer();//考虑到线程安全,此处使用StringBuffer
boolean first = true;       //判断,号的添加

//onCreatView中init 
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(layoutManager.VERTICAL);
recyclerview.setLayoutManager(layoutManager);
recyclerview.addItemDecoration(new SpaceItemDecoration(2));
mAdapter = new Adapter(context,list);
recyclerview.setAdapter(mAdapter);

//在需要用到recyclerview选中的数据时调用:
setSelectedData();
//将选中条目添加到StringBuffer中
private void setSelectedData() {
    Map, Boolean> map = mAdapter.getCheckMap();
    int count = mAdapter.getItemCount();
    //int count = adpAdapter.getCount();
    for (int i = 0; i < count; i++) {
        // 因为List的特性,删除了2个item,则3变成2,所以这里要进行这样的换算,才能拿到删除后真正的position
        int position = i - (count - mAdapter.getItemCount());
        if (map.get(i) != null && map.get(i)) {
            DemoBean  bean = mAdapter.getItemData(position);
            if (first) {
                first = false;
            } else {
                buffer.append(",");
            }
            buffer.append(bean.***);
        }
    }
}
 
  
 

2.Adapter 代码:

public class Adapter extends RecyclerView.Adapter {

    private Context mContext;
    private List mList;
    private Map, Boolean> isCheckMap = new HashMap, Boolean>();
    public Adapter(Context context, List list) {
        mContext = context;
        mList = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.itemlayout, parent, false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.mItemInvitelistIvAvatar.setImage.....;
        holder.mItemTvName.setText(mList.get(position).name)
	
public class MyViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.item_iv_avatar)
        ImageView mItemIvAvatar;
        @BindView(R.id.item_tv_name)
        TextView mItemTvName;
        @BindView(R.id.item_layout)
        LinearLayout mItemLayout;
        @BindView(R.id.item_checkbox)
        CheckBox mItemCheckbox;
        public MyViewHolder(View itemView) {
            super(itemView);
	   ButterKnife.bind(this, itemView);
}
}
    public DemoBean getItemData(int position) {
        return this.mList.get(position);
    }
    public Map, Boolean> getCheckMap() {
        return this.isCheckMap;
    }
}

布局文件:

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/white">
            android:id="@+id/item_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/m60">
                    android:id="@+id/item_iv_avatar"
            android:layout_width="@dimen/m40"
            android:layout_height="@dimen/m40"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/m10"
            />
                    android:id="@+id/item_tv_name"
            android:layout_width="@dimen/m0"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="18sp"
            android:textColor="@color/black"
            />
 
    
            android:id="@+id/item_checkbox"
        android:layout_width="match_parent"
        android:layout_height="@dimen/m60"
        android:button="@null"
        android:focusable="false"/>
   

demo下载地址使用recyclerview+checkbox实现recyclerview item选中效果

你可能感兴趣的:(个人笔记)