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://bzwzjz.com/article/dseogc.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设 企业网站设计 app网站建设 成都网站建设 高端网站设计 定制网站制作 外贸网站建设 成都模版网站建设 网站设计 成都网站建设 成都响应式网站建设公司 重庆外贸网站建设 古蔺网站建设 成都网站建设 营销型网站建设 成都网站建设 H5网站制作 成都网站建设 品牌网站建设 成都网站制作 重庆企业网站建设 企业网站设计