recyclerview給item新增選中效果(多選)
阿新 • • 發佈:2019-01-08
大體實現思路:
使用相對佈局,在recyclerview的item整體佈局底層新增一個CheckBox,在adapter中引用一個Map標記選中CheckBox,然後在bandview中給CheckBox setOnCheckedChangeListener改變CheckBox背景、向Map中新增標記.
demo連結在文末.
具體實現:
1.activity:
RecyclerView recyclerview; Adapter mAdapter; StringBuffer buffer = new StringBuffer();//考慮到執行緒安全,此處使用StringBuffer booleanfirst = 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<Integer, 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<Adapter.MyViewHolder> { private Context mContext; private List<DemoBean> mList; private Map<Integer, Boolean> isCheckMap = new HashMap<Integer, Boolean>(); public Adapter(Context context, List<DemoBean> 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<Integer, Boolean> getCheckMap() { return this.isCheckMap; } }
佈局檔案:
<?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:background="@color/white"> <LinearLayout android:id="@+id/item_layout" android:layout_width="match_parent" android:layout_height="@dimen/m60"> <ImageView 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" /> <TextView 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" /> </LinearLayout> <CheckBox android:id="@+id/item_checkbox" android:layout_width="match_parent" android:layout_height="@dimen/m60" android:button="@null" android:focusable="false"/> </RelativeLayout>
demo下載地址使用recyclerview+checkbox實現recyclerview item選中效果