Android BaseRecyclerViewAdapterHelper拖动和侧滑删除

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为布局文件
    }

    @Override
    protected 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">

    <TextView
        android: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"
        />
    <TextView
        android: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() {
    @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) {}
}

OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
    @Override
    public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {}
    @Override
    public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {}
    @Override
    public 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);

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页