Android Recyclerview实现多选,单选,全选,反选,批量删除的功能

Android Recyclerview实现多选,单选,全选,反选,批量删除的功能,第1张

概述效果图如下:  Recyclerview实现多选,单选,全选,反选,批量删除的步骤1.在Recyclerview布局中添加上底部的全选和反选按钮,删除按钮,和计算数量等控件

效果图如下:

 

 

RecyclervIEw 实现多选,单选,全选,反选,批量删除的步骤

1.在RecyclervIEw布局中添加上底部的全选和反选按钮,删除按钮,和计算数量等控件

2.这里选中的控件没有用checkBox来做,用的是imagevIEw,选中和不选中其实是两张图片

3.默认是不显示选中的控件的,点击编辑的时候显示,点击取消的时候隐藏

4.通过adapter和activity数据之间的传递,然后进行具体的 *** 作

具体代码如下:

在recyclervIEw的布局中写全选,删除,计数等相应的控件

 <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中的布局就不必再写了,就一个item,最左边一个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"/>

布局写完开始写逻辑代码

首先在adapter定义一个方法,以便在activity中拿到数据添加进adapter中

 public voID notifyAdapter(List<MyliveList.Mylive> myliveList,boolean isAdd){  if (!isAdd){   this.mMyliveList=myliveList;  }else {   this.mMyliveList.addAll(myliveList);  }  notifyDataSetChanged(); }

然后在activity中拿到获取到数据后调用adapter中的这个方法,添加数据

 mAdapter.notifyAdapter(data.getList(),false);

在adapter中在判空,更有保证

 public List<MyliveList.Mylive> getMyliveList(){  if (mMyliveList == null) {   mMyliveList =new ArrayList<>();  }  return mMyliveList; }

然后adapter中的getItemCount就直接拿到上面这个mMyliveList的大小就可以了

接下来开始点击编辑的时候显示出imagevIEw和recyclevIEw中的底部全选反选部分

定义两个变量

 private static final int MYliVE_MODE_CHECK = 0; private static final int MYliVE_MODE_EDIT = 1;//点击编辑的时候显示,顺便调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);//当然,adapter中也有先关的变量在记录 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); // 为了条目不复用//显示和隐藏  if (mEditMode == MYliVE_MODE_CHECK) {   holder.mCheckBox.setVisibility(VIEw.GONE);  } else {   holder.mCheckBox.setVisibility(VIEw.VISIBLE);

为了方便记录选中的状态,在bean里面用个变量记起来

 public boolean isSelect;  public boolean isSelect() {   return isSelect;  }  public voID setSelect(boolean isSelect) {   this.isSelect = isSelect;  }//然后点击条目选中和不选中的时候为ImagevIEw设置不同的图片   if(mylive.isSelect()) {    holder.mCheckBox.setimageResource(R.mipmap.ic_checked);   }else{    holder.mCheckBox.setimageResource(R.mipmap.ic_uncheck);   }//在adapter中暴漏一个Item的点击事件的接口 public interface OnSwipeListener {  voID onItemClickListener(int pos,List<MyliveList.Mylive> myliveList); }
/* 在activity中的item点击事件中,来 *** 作ImagevIEw是否选中 *///用一个变量记录 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();
 /**  * 根据选择的数量是否为0来判断按钮的是否可点击.  *  * @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));  } }

至于全选和反选的 *** 作,就是遍历这个bean类,得到他的选择状态,重新设置就可以了.

 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));

最后删除的话就调删除的接口,遍历这个bean,判断当前的状态如果是选中的状态,就删除! 这样就OK了 !!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android Recyclerview实现多选,单选,全选,反选,批量删除的功能全部内容,希望文章能够帮你解决Android Recyclerview实现多选,单选,全选,反选,批量删除的功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1146040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存