问题描述
我有RecyclerView
和所附的itemtouchhelper
,我用它来构建
class itemtouchhelperCallback(
private val adapter: itemtouchhelperAdapter
) : itemtouchhelper.Callback() {
override fun isLongPressDragEnabled(): Boolean {
return true
}
...
}
在每次拖动事件开始上,我都会称振动。在精确的拖动开始时刻。
为此,我在ViewHolder
内设置了touch listener
。然后,我尝试为第一个ACTION_MOVE
事件调用振动。不幸的是,它发生的速度比拖动项目的速度快。我希望振动和移动物品的可能性同时发生。为了给用户准确的触觉反馈,他现在可以拖动和移动项目。
我希望itemtouchhelper
或itemtouchhelper.Callback
公开适当的API。没有。但是我注意到在itemtouchhelper
内部有一个mActionState
,其值在预期的时刻ACTION_STATE_DRAG
(在onLongPress()
内部,所以这两个类应该知道何时开始拖动!可惜他们没有不要公开分享他们的知识。
此外,我注意到similar topic about action drag ended非常多,但在那里找不到action drag started
的解决方案。
解决方法
您需要将此方法添加到您的回调中
override fun onSelectedChanged(viewHolder: ViewHolder?,actionState: Int) {
super.onSelectedChanged(viewHolder,actionState)
if (actionState == ACTION_STATE_DRAG) {
// your code here
}
}
这对我很有效
,最后我发现了骇人听闻的解决方案,它是ItemTouchHelper.Callback()
的一部分。与将getMovementFlags
更改为mActionState
几乎在同一时间调用ACTION_STATE_DRAG
。在onLongPress() of
ItemTouchHelper`中。
打过两次电话,所以要当心;-)
经过compileSdkVersion = 28
不过我希望看到更好的解决方案。