Android Design ItemTouchHelper实现酷炫列表的移行和滑动删除效果

本文介绍了如何利用Android Design库中的ItemTouchHelper实现RecyclerView列表的移行和滑动删除功能。通过ItemTouchHelperViewHolder、ItemTouchHelperAdapter和OnStartDragListener等关键组件,实现长按拖动和左右滑动操作。文章详细讲解了isLongPressDragEnabled、getMovementFlags、onMove和onSwiped等方法的应用,并提供了MyRecycleViewAdapter的实现细节。
摘要由CSDN通过智能技术生成

        ItemTouchHelper主要是作用在RecyclerView上。本文仍在android 5.0新特性 RecyclerView使用初级基础上。可以看到实现的列表右边的ImageView的样式是一般常见的编辑图片 即提示用户可以进行 列表的移行操作。不知道各位看官是否看个我的这篇文章:自定义View(三) switch开关按钮 ViewDragHelper的使用初级 。其实ViewDragHelper很容易实现表格的滑动删除功能。但其更加普遍适用于所有View。而ItemTouchHelper是Android Design 库中专门针对RecyclerView的移动和滑动操作。好了。看看本文的具体实现。由于布局文件与android 5.0新特性 RecyclerView使用初级本文不在给出。

       实现上述功能主要依赖一下辅助类ItemTouchHelperViewHolder、ItemTouchHelperAdapter、OnStartDragListener。(本文主要参考github上开源项目点击打开链接)

1.ItemTouchHelperViewHolder  提供选中和(移行、滑动)手势完成后回调

public interface ItemTouchHelperViewHolder {

    /**
     * Called when the {@link ItemTouchHelper} first registers an item as being moved or swiped.
     * Implementations should update the item view to indicate it's active state.
     */
    void onItemSelected();


    /**
     * Called when the {@link ItemTouchHelper} has completed the move or swipe, and the active item
     * state should be cleared.
     */
    void onItemClear();
}

2 ItemTouchHelperAdapter.java 提供move和dimiss删除接口

public interface ItemTouchHelperAdapter {

    /**
     * Called when an item has been dragged far enough to trigger a move. This is called every time
     * an item is shifted, and <strong>not</strong> at the end of a "drop" event.<br/>
     * <br/>
     * Implementations should call {@link RecyclerView.Adapter#notifyItemMoved(int, int)} after
     * adjusting the underlying data to reflect this move.
     *
     * @param fromPosition The start position of the moved item.
     * @param toPosition   Then resolved position of the moved item.
     * @return True if the item was moved to the new adapter position.
     *
     * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
     * @see RecyclerView.ViewHolder#getAdapterPosition()
     */
    boolean onItemMove(int fromPosition, int toPosition);


    /**
     * Called when an item has been dismissed by a swipe.<br/>
     * <br/>
     * Implementations should call {@link RecyclerView.Adapter#notifyItemRemoved(int)} after
     * adjusting the underlying data to reflect this removal.
     *
     * @param position The position of the item
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值