android查询列表左滑右滑,Android滑动列表(拖拽,左滑删除,右滑完成)功能实现(2)...

ItemTouchHelper类

之前我们实现了滑动列表的一些基本功能,为了实现更多的效果,我们来仔细看一下ItemTouchHelper中的类:

ItemTouchHelper.SimpleCallback

这个主要是帮你写好了getMovementFlags,你只要直接传一些参数即可,看一下怎么用:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

ItemTouchHelper mIth = new ItemTouchHelper(

new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,

ItemTouchHelper.LEFT) {

public abstract boolean onMove(RecyclerView recyclerView,

ViewHolder viewHolder, ViewHolder target) {

final int fromPos = viewHolder.getAdapterPosition();

final int toPos = target.getAdapterPosition();

// move item in `fromPos` to `toPos` in adapter.

return true;// true if moved, false otherwise

}

public void onSwiped(ViewHolder viewHolder, int direction) {

// remove from adapter

}

});

View Code

ItemTouchHelper.ViewDropHandler

An interface which can be implemented by LayoutManager for better integration with

getItemOffsets

void getItemOffsets (Rect outRect,

View view,

RecyclerView parent,

RecyclerView.State state)

Retrieve any offsets for the given item. Each field of outRect specifies the number of pixels that the item view should be inset by, similar to padding or margin. The default implementation sets the bounds of outRect to 0 and returns.

If this ItemDecoration does not affect the positioning of item views, it should set all four fields of outRect (left, top, right, bottom) to zero before returning.

If you need to access Adapter for additional data, you can call

类似于itemDecoration里的getItemOffsets,也是用于给view设置padding的: outRect.set(l,t,r,b)

onDraw

Draw any appropriate decorations into the Canvas supplied to the RecyclerView. Any content drawn by this method will be drawn before the item views are drawn, and will thus appear underneath the views.

还有个onDrawOver方法,不过是用于之后的

startDrag | startSwipe

void startDrag (RecyclerView.ViewHolder viewHolder)

Starts dragging the provided ViewHolder. By default, ItemTouchHelper starts a drag when a View is long pressed. You can disable that behavior by overriding

For this method to work:

The provided ViewHolder must be a child of the RecyclerView to which this ItemTouchHelper is attached.

There must be a previous touch event that was reported to the ItemTouchHelper through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener grabs previous events, this should work as expected.

For example, if you would like to let your user to be able to drag an Item by touching one of its descendants, you may implement it as follows:

viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

if (MotionEvent.getActionMasked(event) == MotionEvent.ACTION_DOWN) {

mItemTouchHelper.startDrag(viewHolder);

}

return false;

}

});

用于开始拖动的一些时间

ItemTouchHelper.Callback

标签:左滑,viewHolder,outRect,右滑,ViewHolder,ItemTouchHelper,Android,RecyclerView,View

来源: https://www.cnblogs.com/cnblogs321114287/p/10569330.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值