资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Androidrecyclerview实现拖拽排序和侧滑删除-创新互联

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

吴兴网站建设公司创新互联,吴兴网站设计制作,有大型网站制作公司丰富经验。已为吴兴成百上千提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的吴兴做网站的公司定做!
private void initveiw() {
  ArrayList 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 ;itoPosition; 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);
  }
}


分享标题:Androidrecyclerview实现拖拽排序和侧滑删除-创新互联
链接分享:http://www.cdkjz.cn/article/dseogc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220