1.适配器
adapter继承BaseItemDraggableAdapte
public class ItemDragAdapter extends BaseItemDraggableAdapter<BsInventoryBeanSub, BaseViewHolder> {//BsInventoryBeanSub是我的Bean类public ItemDragAdapter(List<BsInventoryBeanSub> data) {super(R.layout.layout_instock_sub_item,data);//R.layout.layout_instock_sub_item为布局文件}@Overrideprotected void convert(BaseViewHolder holder, BsInventoryBeanSub inStockBeanSub) {//填充数据holder.setText(R.id.barCodeTv, inStockBeanSub.getBarCode());holder.setText(R.id.qtyTv, inStockBeanSub.getNum());//数量}//绑定数据public void bindData(Collection<BsInventoryBeanSub> data) {if (data != null) {mData = new ArrayList<>(data);notifyDataSetChanged();}}//替换数据,更新界面public void updateData(List<BsInventoryBeanSub> list) {mData.clear();mData.addAll(list);notifyDataSetChanged();}
}
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="30dp"android:background="#fff"android:orientation="horizontal"><TextViewandroid:id="@+id/barCodeTv"android:background="@drawable/border"android:layout_width="0dp"android:textSize="14dp"android:layout_height="match_parent"android:layout_weight="4"android:layout_gravity="center"android:gravity="center"/><TextViewandroid:id="@+id/qtyTv"android:layout_weight="2"android:background="@drawable/border"android:layout_gravity="center_vertical"android:gravity="center"android:textSize="14dp"android:layout_width="0dp"android:layout_height="match_parent" /></LinearLayout>
3.Activity代码
拖拽和滑动删除的回调方法
OnItemDragListener onItemDragListener = new OnItemDragListener() {@Overridepublic void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos){}@Overridepublic void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {}@Overridepublic void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {}
}OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {@Overridepublic void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {}@Overridepublic void clearView(RecyclerView.ViewHolder viewHolder, int pos) {}@Overridepublic void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {//侧滑删除可以在这里进行数据更新ToastUtils.showShort(mInstockSubBeanList.get(pos).getBarCode()+"删除成功");BsInventoryPDDDaoImpl.deleteInstockSub(mInstockSubBeanList.get(pos));mInstockSubBeanList = BsInventoryPDDDaoImpl.findAllInstockSubNyBillNo(mInstockBean);mAdapter.updateData(mInstockSubBeanList);}
};
Adapter设置
//设置数据源
mAdapter = new ItemDragAdapter(mInstockSubBeanList);ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
//设置RecyclerView
itemTouchHelper.attachToRecyclerView(mInvSubRecyclerView);// 开启拖拽
mAdapter.enableDragItem(itemTouchHelper, R.id.barCodeTv, true);
mAdapter.setOnItemDragListener(onItemDragListener);// 开启滑动删除
mAdapter.enableSwipeItem();
mAdapter.setOnItemSwipeListener(onItemSwipeListener);