[英]RecyclerView ItemTouchHelper Action Drag Started
I've got RecyclerView
with attached ItemTouchHelper
, which I construct with我有
RecyclerView
附带的ItemTouchHelper
,我用它构建
class ItemTouchHelperCallback(
private val adapter: ItemTouchHelperAdapter
) : ItemTouchHelper.Callback() {
override fun isLongPressDragEnabled(): Boolean {
return true
}
...
}
I am going to call vibration on every drag event start .我将在每次拖动事件开始时调用振动。 In exact drag start moment.
在确切的拖动开始时刻。
To do it, inside ViewHolder
I set touch listener
.为此,在
ViewHolder
我设置了touch listener
。 Then I tried to call vibration for the first ACTION_MOVE
event.然后我尝试为第一个
ACTION_MOVE
事件调用振动。 Unfortunately, it happens faster than item drag is possible.不幸的是,它发生得比项目拖动更快。 I want vibration and possibility to move items happen at the same time.
我希望振动和移动物品的可能性同时发生。 To give user accurate haptic feedback, that he is able to drag and move items from now on.
为了给用户准确的触觉反馈,从现在开始他可以拖动和移动项目。
I expected ItemTouchHelper
or ItemTouchHelper.Callback
to expose appropriate API.我希望
ItemTouchHelper
或ItemTouchHelper.Callback
公开适当的 API。 It doesn't.它没有。 But I noticed that inside
ItemTouchHelper
there's mActionState
with value ACTION_STATE_DRAG
at the expected moment (inside onLongPress()
So those 2 classes should know when drag starts! It's a pity they don't share their knowledge publicly.但我注意到,里面
ItemTouchHelper
有mActionState
与价值ACTION_STATE_DRAG
在预期的时刻(内onLongPress()
因此,那些2班应该知道什么时候开始拖!这是他们没有公开分享他们的知识可惜。
Also, I've noticed very similar topic about action drag ended but I couldn't find solution to action drag started
there.另外,我注意到关于动作拖动结束的非常相似的主题,但我找不到
action drag started
那里action drag started
解决方案。
You need to add this method to your callback您需要将此方法添加到您的回调中
override fun onSelectedChanged(viewHolder: ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
if (actionState == ACTION_STATE_DRAG) {
// your code here
}
}
This works well for me这对我很有效
I found hacky solution in the end, which is part of ItemTouchHelper.Callback()
.我最终找到了 hacky 解决方案,它是
ItemTouchHelper.Callback()
一部分。 getMovementFlags
is called in almost the same moment like change mActionState
to ACTION_STATE_DRAG
. getMovementFlags
几乎在同一时刻被调用,就像将mActionState
更改为ACTION_STATE_DRAG
。 Inside onLongPress() of
ItemTouchHelper`.在
onLongPress() of
。
Called twice, so watch out for that ;-)打了两次电话,所以要小心;-)
Tested on compileSdkVersion = 28
在
compileSdkVersion = 28
上测试
I'd like to see better solution though.不过,我希望看到更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.