RecyclerView的侧滑删除和长按交换位置

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wang_k516/article/details/80974220

RecyclerView的使用越来越广泛,同时围绕条目列表的功能操作也很多,通过ItemTouchHelper可以对条目进行滑动删除和长按交换位置。

首先,定义一个删除和改变条目的接口,在adapter实现

public interface ItemTouchHelperInter {
    //调换位置
    void onItemChange(int fromPos, int toPos);
    //滑动删除条目
    void onItemDelete(int pos);
}
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ItemViewHolder> implements ItemTouchHelperInter {

    private List<String> mData;
    private Context context;

    public TestAdapter(List list, Context context) {
        this.mData = list;
        this.context = context;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        holder.txtItem.setText(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public void onItemChange(int fromPos, int toPos) {
        Collections.swap(mData, fromPos, toPos);//调换数据源位置
        notifyItemMoved(fromPos, toPos);//调换条目位置
    }

    @Override
    public void onItemDelete(int pos) {
        mData.remove(pos);
        notifyItemRemoved(pos);
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView txtItem;

        public ItemViewHolder(View itemView) {
            super(itemView);
            txtItem = itemView.findViewById(R.id.item);
        }
    }
}

重写ItemTouchHelper.Callback的相关方法

public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private ItemTouchHelperInter mAdapter;

    public ItemTouchHelperCallback(ItemTouchHelperInter mAdapter) {
        this.mAdapter = mAdapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//上下拖动
        int swipeFlag = ItemTouchHelper.LEFT;//从右向左
        return makeMovementFlags(dragFlag, swipeFlag);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        mAdapter.onItemChange(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;//返回true 表示目标viewholder已经移到目标位置
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;//返回true支持长按拖动 false不支持
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;//返回true支持滑动 false不支持
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        mAdapter.onItemDelete(viewHolder.getAdapterPosition());
    }
}

绑定Recyclerview

 private void initData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            String s = "条目" + i;
            mList.add(s);
        }
    }

    private void initView() {
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        TestAdapter testAdapter = new TestAdapter(mList,this);
        ItemTouchHelperCallback helperCallback = new ItemTouchHelperCallback(testAdapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(helperCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
        recyclerView.setAdapter(testAdapter);
    }

 

 

 

展开阅读全文

没有更多推荐了,返回首页