实现拖拽
- 继承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 = 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);
网友评论