简体   繁体   English

RecyclerView ItemTouchHelper 动作拖动开始

[英]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.我希望ItemTouchHelperItemTouchHelper.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.但我注意到,里面ItemTouchHelpermActionState与价值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 = 28compileSdkVersion = 28上测试

I'd like to see better solution though.不过,我希望看到更好的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView ItemTouchHelper 动作拖动结束 - RecyclerView ItemTouchHelper Action Drag Ended 使用 Recyclerview 中的 itemTouchHelper 在 cardview 上拖动高程平移z - Android drag elevation translationz on cardview with itemTouchHelper in Recyclerview Android - 使用 ItemTouchHelper.Callback 拖动 RecyclerView - Android - RecyclerView drag using ItemTouchHelper.Callback "在 NestedScrollView 中带有 RecyclerView 的 ItemTouchHelper:拖动滚动不起作用" - ItemTouchHelper with RecyclerView in NestedScrollView: Drag scrolling not work RecyclerView ItemTouchHelper滑动操作在Fragment Class中不起作用 - RecyclerView ItemTouchHelper swipe action is not working in Fragment Class 拖动时如何在使用 ItemTouchHelper 时取消 RecyclerView 中项目的拖动? - How to cancel dragging of items in RecyclerView when using ItemTouchHelper, as you drag? 使用 itemtouchhelper、recyclerview 和 Firestore 拖放项目,例如 Google Tasks - Drag & drop items with itemtouchhelper, recyclerview and Firestore like Google Tasks RecyclerView通过itemTouchHelper拖放,拖动时快速拖动 - RecyclerView drag & drop via itemTouchHelper bahaving strange when dragging fast 在拖动/重新排序期间调整itemTouchhelper上的所有recyclerview单元格的大小 - Resize all recyclerview cells on itemTouchhelper during drag/reorder android - 如何捕捉与 RecyclerView 一起使用的 ItemTouchHelper 的 Drop 动作 - android - how to catch Drop action of ItemTouchHelper which is used with RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM