成都创新互联网站制作重庆分公司

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实现拖拽排序和侧滑删除-创新互联
URL标题:http://cxhlcq.com/article/dseogc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部