美文网首页
BaseQuickAdapter实现拖拽排序或多布局

BaseQuickAdapter实现拖拽排序或多布局

作者: 天青色等Y雨 | 来源:发表于2020-12-11 18:10 被阅读0次

    实现拖拽

    • 继承BaseItemDraggableAdapter
    // 适配器继承
    public class EditDataCenterAdapter extends BaseItemDraggableAdapter<Bean, BaseViewHolder>{
    }
    
    // 代码中设置绑定适配器
    mAdapter = new EditDataCenterAdapter(mList);
    mRvList.setLayoutManager(new LinearLayoutManager(this));
    mRvList.setAdapter(mAdapter);
    
    ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
    itemTouchHelper.attachToRecyclerView(mRvList);
    // 设置可拖拽(rl_layout: item的根布局id)
    mAdapter.enableDragItem(itemTouchHelper, R.id.rl_layout, true);
    mAdapter.setOnItemDragListener(onItemDragListener);
    
    • onItemDragListener
    OnItemDragListener onItemDragListener = new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
            }
    
            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
            }
    
            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
            }
        };
    

    实现多布局

    • 适配器实现MultiItemEntity, 实现方法,根据后台返回type值
    public static class AdapterBean implements MultiItemEntity {
              @Override
              public int getItemType() {
                     return type;
              }
    }
    
    • 适配器继承BaseMultiItemQuickAdapter
    // 构造方法中添加多布局
    public class CustomFieldAdapter extends BaseMultiItemQuickAdapter<AdapterBean , BaseViewHolder> {
            public CustomFieldAdapter(List<CustomFieldVO.CustomFieldListBean> data) {
                    super(data);
                    // type1 2 3要与适配器Bean中getItemType()返回的对应
                    addItemType(type1, R.layout.item_layout1);
                    addItemType(type2, R.layout.item_layout2);
                    addItemType(type3, R.layout.item_layout3);
             }
    }
    
    • 代码中绑定适配器
    CustomFieldAdapter  mAdapter = new CustomFieldAdapter (mList);
    mAdapter.setOnItemChildClickListener(this);
    mRvList.setLayoutManager(new LinearLayoutManager(this));
    mRvList.setAdapter(mBaseCustomFieldAdapter);
    // 拿到新数据之后绑定数据
    mAdapter.setNewData(mList);
    

    相关文章

      网友评论

          本文标题:BaseQuickAdapter实现拖拽排序或多布局

          本文链接:https://www.haomeiwen.com/subject/aatsgktx.html