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

Android recyclerview实现拖拽排序和侧滑删除

程序员文章站 2023-12-19 17:51:04
recyclerview现在基本已经替代listview了,recyclerview也越来越好用了  当我们有实现条目的拖拽排序和侧滑删除时  可以直接...

recyclerview现在基本已经替代listview了,recyclerview也越来越好用了  当我们有实现条目的拖拽排序和侧滑删除时  可以直接时候recyclerview提供的api就可以直接实现了
先贴上主要代码

private void initveiw() {
  arraylist<string> items = new arraylist<>(arrays.aslist("itme1", "item2", "itme3", "item4", "item5", "item6", "item7", "item8", "itme9", "item10", "itme11", "item12", "item13", "item14", "item15", "item16"));
  recyclerview.setlayoutmanager(new linearlayoutmanager(this,linearlayoutmanager.vertical,false));
  simpleadapter adapter = new simpleadapter(items);
  recyclerview.setadapter(adapter);
  itemtouchhelper helper = new itemtouchhelper(new myitemtouchcallback(adapter));
  helper.attachtorecyclerview(recyclerview);
}

public class myitemtouchcallback extends itemtouchhelper.callback{

  private simpleadapter adapter;

  public myitemtouchcallback(simpleadapter adapter) {
    this.adapter = adapter;
  }

  @override
  public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) {
    int dragflag;
    int swipeflag;
    recyclerview.layoutmanager manager = recyclerview.getlayoutmanager();
    if (manager instanceof gridlayoutmanager){
      dragflag = itemtouchhelper.down | itemtouchhelper.up | itemtouchhelper.left | itemtouchhelper.right;
      swipeflag = 0;
    }else{
      dragflag = itemtouchhelper.down | itemtouchhelper.up;
      swipeflag = itemtouchhelper.end | itemtouchhelper.start;
    }

    return makemovementflags(dragflag,swipeflag);
  }

  @override
  public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) {
    int fromposition = viewholder.getadapterposition();
    int toposition = target.getadapterposition();
    if (fromposition < toposition ){
      for (int i = fromposition ;i<toposition ;i++){
        collections.swap(adapter.getdatalist(),i,i+1);
      }
    }else{
      for (int i= fromposition; i>toposition; i--){
        collections.swap(adapter.getdatalist(),i ,i-1);
      }
    }
    recyclerview.getadapter().notifyitemmoved(fromposition,toposition);
    return true;
  }

  @override
  public void onswiped(recyclerview.viewholder viewholder, int direction) {
    int position = viewholder.getadapterposition();
    if (direction == itemtouchhelper.end | direction==itemtouchhelper.start){
      adapter.getdatalist().remove(position);
      adapter.notifyitemremoved(position);
    }
  }

  @override
  public void onselectedchanged(recyclerview.viewholder viewholder, int actionstate) {
    super.onselectedchanged(viewholder, actionstate);
    if (actionstate==itemtouchhelper.action_state_drag){
      viewholder.itemview.setbackgroundcolor(color.blue);
    }

  }

  @override
  public void clearview(recyclerview recyclerview, recyclerview.viewholder viewholder) {
    super.clearview(recyclerview, viewholder);
    viewholder.itemview.setbackgroundcolor(0);
  }
}

步骤:

创建 itemtouchhelper 对象时候,需要我们传入一个实现了 itemtouchhelper.callback 接口的对象。而排序和删除的逻辑都封装在了这个 itemtouchhelper.callback 的对象里面了。

private void initveiw() {
  arraylist<string> items = new arraylist<>(arrays.aslist("itme1", "item2", "itme3", "item4", "item5", "item6", "item7", "item8", "itme9", "item10", "itme11", "item12", "item13", "item14", "item15", "item16"));
  recyclerview.setlayoutmanager(new linearlayoutmanager(this,linearlayoutmanager.vertical,false));
  simpleadapter adapter = new simpleadapter(items);
  recyclerview.setadapter(adapter);
  itemtouchhelper helper = new itemtouchhelper(new myitemtouchcallback(adapter));
  helper.attachtorecyclerview(recyclerview);
}

 实现itemtouchhelper.callback 接口后有三个方法需要重写:

getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) :设置滑动类型的标记。需要设置两种类型的 flag ,即 dragflags 和 swipeflags ,分别代表着拖拽标记和滑动标记。最后需要调用 makemovementflags(dragflags,
 swipeflags)方法来合成返回。
onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) :当用户拖拽列表某个 item 时会回调。很明显,拖拽排序的代码应该在这个方法中实现。

onswiped(recyclerview.viewholder viewholder, int direction) :当用户滑动列表某个 item 时会回调。所以侧滑删除的代码应该在这个方法中实现。
下面是重写的几个方法:

第一个,getmovementflags方法

@override
public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) {
  int dragflag;
  int swipeflag;
  recyclerview.layoutmanager manager = recyclerview.getlayoutmanager();
  if (manager instanceof gridlayoutmanager){
    dragflag = itemtouchhelper.down | itemtouchhelper.up | itemtouchhelper.left | itemtouchhelper.right;
    swipeflag = 0;
  }else{
    dragflag = itemtouchhelper.down | itemtouchhelper.up;
    swipeflag = itemtouchhelper.end | itemtouchhelper.start;
  }

  return makemovementflags(dragflag,swipeflag);
}

这个方法里面根据layoutmanager分了两种情况,根据自己的情况去分
在gridlayoutmanager中只能上下左右拖拽但是不能侧滑删除,所以swipflag = 0;swipeflag的值itemtouchhelper.end是右滑删除,itemtouchhelper.start是左滑删除
最后调用makemovementflags方法合成返回

第二个,onmove方法

@override
public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) {
  int fromposition = viewholder.getadapterposition();
  int toposition = target.getadapterposition();
  if (fromposition < toposition ){
    for (int i = fromposition ;i<toposition ;i++){
      collections.swap(adapter.getdatalist(),i,i+1);
    }
  }else{
    for (int i= fromposition; i>toposition; i--){
      collections.swap(adapter.getdatalist(),i ,i-1);
    }
  }
  recyclerview.getadapter().notifyitemmoved(fromposition,toposition);
  return true;
}

这个方法是用户在拖拽 item 的时候调用。所以关于列表排序的代码应该写在这里。方法参数中的 viewholder 代表的是用户当前拖拽的 item ,而 target 代表的是被用户拖拽所覆盖的那个 item 。所以在 onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) 方法中的逻辑就是把 fromposition 至 toposition 为止改变它们的位置。

第三个,onswiped方法

@override
public void onswiped(recyclerview.viewholder viewholder, int direction) {
  int position = viewholder.getadapterposition();
  if (direction == itemtouchhelper.end | direction==itemtouchhelper.start){
    adapter.getdatalist().remove(position);
    adapter.notifyitemremoved(position);
  }
}

这个方法是在用户侧滑的时候调用的 ,在里面对adapter的数据进行删除就可以了

最后两个方法

 @override
  public void onselectedchanged(recyclerview.viewholder viewholder, int actionstate) {
    super.onselectedchanged(viewholder, actionstate);
    if (actionstate==itemtouchhelper.action_state_drag){
      viewholder.itemview.setbackgroundcolor(color.blue);
    }

  }

  @override
  public void clearview(recyclerview recyclerview, recyclerview.viewholder viewholder) {
    super.clearview(recyclerview, viewholder);
    viewholder.itemview.setbackgroundcolor(0);
  }
}

就是优化的方法,第一个是选择条目改变状态,第二个是手指抬起之后恢复条目状态

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

上一篇:

下一篇: