欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android利用RecyclerView实现全选、置顶和拖拽功能示例

程序员文章站 2023-11-13 13:00:52
前言 今天给大家分享是如何在recyclerview实现全选,itemtouchhelper实现侧滑删除,拖拽功能。比较基础。关于recyclerview的强大,就不多说...

前言

今天给大家分享是如何在recyclerview实现全选,itemtouchhelper实现侧滑删除,拖拽功能。比较基础。关于recyclerview的强大,就不多说了。在android l sdk发布的新api中最有意思的就是recyclerview 和 cardview了, 按照官方的说法, recyclerview 一个listview 的一个更高级更灵活的一个版本, 可以自定义的东西太多了。

效果:

Android利用RecyclerView实现全选、置顶和拖拽功能示例
recyclerview实现全选,itemtouchhelper实现侧滑删除,拖拽功能

使用recyclerview,首先我们需要依赖

 compile 'com.android.support:recyclerview-v7:23.+'

项目结构:

Android利用RecyclerView实现全选、置顶和拖拽功能示例
项目结构

主要是把选择的存储在hashmap记录下来,通知用eventbus发送。下面我们一步一步来实现这种效果.

方法如下

1、我们新建一个mainactivity 布局

public class mainactivity extends activity {
 private recyclerview recyclerview;
 private checkbox checkbox;
 private textview selected;
 private listadapter adapter;
 private eventbus event;
 private boolean ischange = false;
 private arraylist<book> list = new arraylist<>();

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initview();
  initdata();
 }

 private void initview() {
  event = eventbus.getdefault();
  event.register(this);
  recyclerview = (recyclerview) findviewbyid(r.id.recyclerview);
  checkbox = (checkbox) findviewbyid(r.id.checkbox);
  selected = (textview) findviewbyid(r.id.selected);
 }

 private void initdata() {
  for (int i = 0; i < 20; i++) {
   book model = new book();
   model.setid(i);
   model.setname("商品" + i);
   model.setdesc("描述" + i);
   list.add(model);
  }
  adapter = new listadapter(list, event);
  recyclerview.sethasfixedsize(true);
  recyclerview.setadapter(adapter);
  recyclerview.setlayoutmanager(new gridlayoutmanager(this, 3));
  checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
   @override
   public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
    try {
     hashmap<integer, boolean> map = new hashmap<integer, boolean>();
     int count = 0;
     if (ischecked) {
      ischange = false;
     }
     for (int i = 0, p = list.size(); i < p; i++) {
      if (ischecked) {
       map.put(i, true);
       count++;
      } else {
       if (!ischange) {
        map.put(i, false);
        count = 0;
       } else {
        map = adapter.getmap();
        count = map.size();
       }
      }
     }
     selected.settext("已选" + count + "项");
     adapter.setmap(map);
    } catch (exception e) {
     e.printstacktrace();
    }
   }
  });

  adapter.setonitemclicklistener(new listadapter.itemclicklistener() {
   @override
   public void onitemclick(recyclerview.viewholder holder, int positon) {
    log.e("onitemclick", "" + positon);
   }

   @override
   public void onitemlongclick(final recyclerview.viewholder holder, final int positon) {
    log.e("onitemlongclick", "" + positon);
   }
  });
 }

 public void oneventmainthread(selectevent event) {
  int size = event.getsize();
  if (size < list.size()) {
   ischange = true;
   checkbox.setchecked(false);
  } else {
   checkbox.setchecked(true);
   ischange = false;
  }
  selected.settext("已选" + size + "项");
 }
 @override
 protected void ondestroy() {
  super.ondestroy();
  event.unregister(this);
 }
}

2、我们建一个listadapter适配器

public class listadapter extends recyclerview.adapter<listadapter.itemviewholder> {

 private list<book> mitems;
 private list<book> selected;
 public hashmap<integer, boolean> map;
 private eventbus eventbus;

 public listadapter(list<book> mitems, eventbus eventbus) {
  this.mitems = mitems;
  this.eventbus = eventbus;
  map = new hashmap<>();
  selected = new arraylist<>();
  init();
 }

 private void init() {
  if (null == mitems || mitems.size() <= 0) {
   return;
  }
  for (int i = 0, p = mitems.size(); i < p; i++) {
   map.put(i, false);
  }
 }

 @override
 public itemviewholder oncreateviewholder(viewgroup parent, int viewtype) {
  view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.activity_main_item, parent, false);
  itemviewholder itemviewholder = new itemviewholder(view);
  return itemviewholder;
 }

 @override
 public void onbindviewholder(final itemviewholder holder, final int position) {
  if (null == mitems || mitems.size() <= 0) {
   return;
  }
  holder.name.settext(mitems.get(position).getname());
  holder.desc.settext(mitems.get(position).getdesc());

  holder.checkbox.settag(new integer(position));//防止划回来时选中消失

  if (map != null) {
   ((itemviewholder) holder).checkbox.setchecked((map.get(position)));
  } else {
   ((itemviewholder) holder).checkbox.setchecked(false);
  }
  holder.checkbox.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
    int mflags = (integer) view.gettag();
    if (map != null) {
     if (map.get(position)) {
      map.put(position, false);
      eventbus.post(new selectevent(selected(map)));
     } else {
      map.put(mflags, boolean.true);
      eventbus.post(new selectevent(selected(map)));
     }
    }
   }
  });
  holder.itemview.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    mitemclicklistener.onitemclick(holder,holder.getadapterposition());
   }
  });
  holder.itemview.setonlongclicklistener(new view.onlongclicklistener() {
   @override
   public boolean onlongclick(view v) {
    mitemclicklistener.onitemlongclick(holder,holder.getadapterposition());
    return true;
   }
  });
 }

 private int selected(hashmap<integer, boolean> map) {
  int size = 0;
  for (integer key : map.keyset()) {
   if(map.get(key)){
    size++;
   }
  }
  return size;
 }
 @override
 public int getitemcount() {
  return mitems == null? 0 :mitems.size();
 }
 public static class itemviewholder extends recyclerview.viewholder{

  public final checkbox checkbox;
  public final textview name;
  public final textview desc;

  public itemviewholder(view itemview) {
   super(itemview);
   checkbox = (checkbox) itemview.findviewbyid(r.id.checkbox);
   name = (textview) itemview.findviewbyid(r.id.tv_name);
   desc = (textview) itemview.findviewbyid(r.id.tv_desc);
  }
 }

 public hashmap<integer, boolean> getmap() {
  return map;
 }

 public void setmap(hashmap<integer, boolean> map) {
  this.map = map;
  notifydatasetchanged();
 }

 /**
  * 点击事件和长按事件
  */
 public interface itemclicklistener{
  void onitemclick(recyclerview.viewholder holder , int position);
  void onitemlongclick(recyclerview.viewholder holder , int position);
 }

 private itemclicklistener mitemclicklistener;
 public void setonitemclicklistener(itemclicklistener listener){
  this.mitemclicklistener=listener;
 }
}

3、我们新建实体book和selectevent

package com.aikaifa.checkall.bean;

public class book {

 private int id;
 private string name;
 private string desc;

 public string getdesc() {
  return desc;
 }

 public void setdesc(string desc) {
  this.desc = desc;
 }

 public string getname() {
  return name;
 }

 public void setname(string name) {
  this.name = name;
 }

 public int getid() {
  return id;
 }

 public void setid(int id) {
  this.id = id;
 }

 public book() {
 }
}

public class selectevent {
 private int size;

 public selectevent(int size) {
  this.size = size;
 }

 public int getsize() {
  return size;
 }
}

4、建立checkbox_selector.xml选中样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@mipmap/checkbox_pressed" android:state_checked="true"/>
 <item android:drawable="@mipmap/checkbox_normal" android:state_checked="false"/>
 <item android:drawable="@mipmap/checkbox_normal"/>
</selector>

5、建立一个activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <checkbox
   android:id="@+id/checkbox"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:drawablepadding="10dp"
   android:text="全选"
   android:textcolor="#969696"
   android:textsize="12sp" />

  <textview
   android:id="@+id/selected"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginleft="10dp"
   android:gravity="center_vertical"
   android:text="已选0项" />
 </linearlayout>

 <android.support.v7.widget.recyclerview
  android:id="@+id/recyclerview"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
</linearlayout>

activity_main_item布局

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/item"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margintop="10dp"
 android:clickable="true"
 android:focusable="true">

 <relativelayout
  android:id="@+id/rl_app"
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:layout_centerhorizontal="true">

  <imageview
   android:id="@+id/iv"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/ic_launcher" />

  <checkbox
   android:id="@+id/checkbox"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignparentright="true"
   android:layout_gravity="center_vertical"
   android:button="@drawable/checkbox_selector" />

 </relativelayout>

 <textview
  android:id="@+id/tv_name"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/rl_app"
  android:gravity="center"
  android:text="name"
  android:textcolor="#ffffff" />

 <textview
  android:id="@+id/tv_desc"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/tv_name"
  android:gravity="center"
  android:textcolor="#f5f5f5"
  android:textsize="9sp" />
</relativelayout>

这样关于recyclerview实现全选,itemtouchhelper实现侧滑删除,拖拽功能就完成了。

示例代码下载:

项目地址:https://github.com/88ios/recyclerviewcheckall

本地下载:点击这里

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。