您好,登錄后才能下訂單哦!
效果圖如下:
Recyclerview 實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的步驟
1.在Recyclerview布局中添加上底部的全選和反選按鈕,刪除按鈕,和計(jì)算數(shù)量等控件
2.這里選中的控件沒(méi)有用checkbox來(lái)做,用的是imageview,選中和不選中其實(shí)是兩張圖片
3.默認(rèn)是不顯示選中的控件的,點(diǎn)擊編輯的時(shí)候顯示,點(diǎn)擊取消的時(shí)候隱藏
4.通過(guò)adapter和activity數(shù)據(jù)之間的傳遞,然后進(jìn)行具體的操作
具體代碼如下:
在recyclerview的布局中寫(xiě)全選,反選,刪除,計(jì)數(shù)等相應(yīng)的控件
<LinearLayout android:id="@+id/ll_mycollection_bottom_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="bottom" android:visibility="gone" android:background="@color/app_bg"> <View android:background="#e5e5e5" android:layout_width="match_parent" android:layout_height="1px"/> <RelativeLayout android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/px_90"> <TextView android:layout_centerVertical="true" android:id="@+id/tv" android:textColor="#1A1A1A" android:textSize="@dimen/px_28" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/px_30" android:text="@string/mine_certify_select" /> <TextView android:layout_centerVertical="true" android:layout_toRightOf="@+id/tv" android:textColor="#1A1A1A" android:textSize="@dimen/px_28" android:id="@+id/tv_select_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/px_18" android:text="0" /> <Button android:textColor="@color/color_b7b8bd" android:textSize="@dimen/px_28" android:layout_centerVertical="true" android:background="@drawable/button__noclickable_shape" android:gravity="center" android:id="@+id/btn_delete" android:layout_width="@dimen/px_160" android:layout_height="@dimen/px_66" android:layout_marginRight="@dimen/px_30" android:layout_alignParentRight="true" android:text="刪除" /> <TextView android:layout_centerVertical="true" android:id="@+id/select_all" android:layout_marginRight="@dimen/px_30" android:background="@drawable/bg_selete_all" android:layout_toLeftOf="@+id/btn_delete" android:layout_width="@dimen/px_160" android:layout_height="@dimen/px_66" android:text="全選" android:gravity="center" android:textColor="#000001" android:textSize="@dimen/px_28"/> </RelativeLayout> </LinearLayout>
Adapter中的布局就不必再寫(xiě)了,就一個(gè)item,最左邊一個(gè)imageview.
<ImageView android:id="@+id/check_box" android:src="@mipmap/ic_uncheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/px_24" android:gravity="center" android:visibility="gone"/>
布局寫(xiě)完開(kāi)始寫(xiě)邏輯代碼
首先在adapter定義一個(gè)方法,以便在activity中拿到數(shù)據(jù)添加進(jìn)adapter中
public void notifyAdapter(List<MyLiveList.MyLive> myLiveList,boolean isAdd){ if (!isAdd){ this.mMyLiveList=myLiveList; }else { this.mMyLiveList.addAll(myLiveList); } notifyDataSetChanged(); }
然后在activity中拿到獲取到數(shù)據(jù)后調(diào)用adapter中的這個(gè)方法,添加數(shù)據(jù)
mAdapter.notifyAdapter(data.getList(), false);
在adapter中在判空,更有保證
public List<MyLiveList.MyLive> getMyLiveList(){ if (mMyLiveList == null) { mMyLiveList =new ArrayList<>(); } return mMyLiveList; }
然后adapter中的getItemCount就直接拿到上面這個(gè)mMyLiveList的大小就可以了
接下來(lái)開(kāi)始點(diǎn)擊編輯的時(shí)候顯示出imageview和recycleview中的底部全選反選部分
定義兩個(gè)變量
private static final int MYLIVE_MODE_CHECK = 0; private static final int MYLIVE_MODE_EDIT = 1; //點(diǎn)擊編輯的時(shí)候顯示,順便調(diào)mAdapter.setEditMode(mEditMode);賦值 mEditMode = mEditMode == MYLIVE_MODE_CHECK ? MYLIVE_MODE_EDIT : MYLIVE_MODE_CHECK; if (mEditMode == MYLIVE_MODE_EDIT) { activity_btn.setText("取消"); ll_mycollection_bottom_dialog.setVisibility(View.VISIBLE); editorStatus = true; } else { activity_btn.setText("編輯"); ll_mycollection_bottom_dialog.setVisibility(View.GONE); editorStatus = false; onRefresh(); } mAdapter.setEditMode(mEditMode); //當(dāng)然,adapter中也有先關(guān)的變量在記錄 private static final int MYLIVE_MODE_CHECK = 0; int mEditMode = MYLIVE_MODE_CHECK; public void setEditMode(int editMode) { mEditMode = editMode; notifyDataSetChanged(); } //在onBindViewHolder中做顯示和隱藏的操作. holder.setIsRecyclable(false); // 為了條目不復(fù)用 //顯示和隱藏 if (mEditMode == MYLIVE_MODE_CHECK) { holder.mCheckBox.setVisibility(View.GONE); } else { holder.mCheckBox.setVisibility(View.VISIBLE);
為了方便記錄選中的狀態(tài),在bean里面用個(gè)變量記起來(lái)
public boolean isSelect; public boolean isSelect() { return isSelect; } public void setSelect(boolean isSelect) { this.isSelect = isSelect; } //然后點(diǎn)擊條目選中和不選中的時(shí)候?yàn)镮mageview設(shè)置不同的圖片 if(myLive.isSelect()) { holder.mCheckBox.setImageResource(R.mipmap.ic_checked); }else{ holder.mCheckBox.setImageResource(R.mipmap.ic_uncheck); } //在adapter中暴漏一個(gè)Item的點(diǎn)擊事件的接口 public interface OnSwipeListener { void onItemClickListener(int pos,List<MyLiveList.MyLive> myLiveList); }
/* 在activity中的item點(diǎn)擊事件中,來(lái)操作Imageview是否選中 */ //用一個(gè)變量記錄 private int index = 0; MyLive myLive = myLiveList.get(pos); boolean isSelect = myLive.isSelect(); if (!isSelect) { index++; myLive.setSelect(true); if (index == myLiveList.size()) { isSelectAll = true; selectAll.setText("取消全選"); } } else { myLive.setSelect(false); index--; isSelectAll = false; selectAll.setText("全選"); } setBtnBackground(index); tv_select_num.setText(String.valueOf(index)); radioAdapter.notifyDataSetChanged();
/** * 根據(jù)選擇的數(shù)量是否為0來(lái)判斷按鈕的是否可點(diǎn)擊. * * @param size */ private void setBtnBackground(int size) { if (size != 0) { mBtnDelete.setBackgroundResource(R.drawable.button_shape); mBtnDelete.setEnabled(true); mBtnDelete.setTextColor(Color.WHITE); } else { mBtnDelete.setBackgroundResource(R.drawable.button__noclickable_shape); mBtnDelete.setEnabled(false); mBtnDelete.setTextColor(ContextCompat.getColor(this, R.color.color_b7b8bd)); } }
至于全選和反選的操作,就是遍歷這個(gè)bean類(lèi),得到他的選擇狀態(tài),重新設(shè)置就可以了.
if (radioAdapter == null) return; if (!isSelectAll) { for (int i = 0, j = radioAdapter.getMyLiveList().size(); i < j; i++) { radioAdapter.getMyLiveList().get(i).setSelect(true); } index = radioAdapter.getMyLiveList().size(); mBtnDelete.setEnabled(true); selectAll.setText("取消全選"); isSelectAll = true; } else { for (int i = 0, j = radioAdapter.getMyLiveList().size(); i < j; i++) { radioAdapter.getMyLiveList().get(i).setSelect(false); } index = 0; mBtnDelete.setEnabled(false); selectAll.setText("全選"); isSelectAll = false; } radioAdapter.notifyDataSetChanged(); setBtnBackground(index); tv_select_num.setText(String.valueOf(index));
最后刪除的話就調(diào)刪除的接口,遍歷這個(gè)bean,判斷當(dāng)前的狀態(tài)如果是選中的狀態(tài),就刪除! 這樣就OK了 !!!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。