简体   繁体   English

RecyclerView ItemTouchHelper.Callback:拖动交换条件

[英]RecyclerView ItemTouchHelper.Callback: Dragging swap condition

I want to implement dragging the cards in such a way that the rearrangement of the cards starts when the card that I am dragging does not completely overlap the element, but only 50%.我想以这样一种方式实现卡片的拖动,当我拖动的卡片没有完全重叠元素时,卡片的重新排列开始,但只有 50%。

Check out an example:看一个例子:

例子

Now, for the right card to move to the left, I need to completely overlap it with the one I am dragging.现在,要让右卡向左移动,我需要将它与我拖动的卡完全重叠。

I tried overriding this method from ItemTouchHelper.Callback:我尝试从 ItemTouchHelper.Callback 覆盖此方法:

 public float getMoveThreshold(@NonNull ViewHolder viewHolder) {
     return .5f;
 }

But it didn't help.但这没有帮助。

So how can I make the swap happen at 50% and not at 100% overlap?那么我怎样才能使交换发生在 50% 而不是 100% 重叠?

After many attempts, I found the solution myself:经过多次尝试,我自己找到了解决方案:

例子

You need to override the chooseDropTarget method from ItemTouchHelper.Callback.您需要覆盖 ItemTouchHelper.Callback 中的 chooseDropTarget 方法。

This is how I did it:我是这样做的:

  //  x ≥ 0.5 (if less than 0.5, it will shake due to constant overlap)
val DRAG_THRESHOLD_PERSENT = 0.5
override fun chooseDropTarget(
    selected: ViewHolder,
    targets: MutableList<ViewHolder>,
    curX: Int, curY: Int
): ViewHolder? {
    val verticalOffset = (selected.itemView.height * DRAG_THRESHOLD_PERSENT).toInt()
    val horizontalOffset = (selected.itemView.width * DRAG_THRESHOLD_PERSENT).toInt()
    val left = curX - horizontalOffset
    val right = curX + selected.itemView.width + horizontalOffset
    val top = curY - verticalOffset
    val bottom = curY + selected.itemView.height + verticalOffset
    var winner: ViewHolder? = null
    var winnerScore = -1
    val dx = curX - selected.itemView.left
    val dy = curY - selected.itemView.top
    val targetsSize = targets.size
    for (i in 0 until targetsSize) {
        val target = targets[i]
        if (dx > 0) {
            val diff = target.itemView.right - right
            if (diff < 0 && target.itemView.right > selected.itemView.right) {
                val score = abs(diff)
                if (score > winnerScore) {
                    winnerScore = score
                    winner = target
                }
            }
        }
        if (dx < 0) {
            val diff = target.itemView.left - left
            if (diff > 0 && target.itemView.left < selected.itemView.left) {
                val score = abs(diff)
                if (score > winnerScore) {
                    winnerScore = score
                    winner = target
                }
            }
        }
        if (dy < 0) {
            val diff = target.itemView.top - top
            if (diff > 0 && target.itemView.top < selected.itemView.top) {
                val score = abs(diff)
                if (score > winnerScore) {
                    winnerScore = score
                    winner = target
                }
            }
        }
        if (dy > 0) {
            val diff = target.itemView.bottom - bottom
            if (diff < 0 && target.itemView.bottom > selected.itemView.bottom) {
                val score = abs(diff)
                if (score > winnerScore) {
                    winnerScore = score
                    winner = target
                }
            }
        }
    }
    return winner
}

How it works:这个怎么运作:
I am calculating the offset of the selected view using the factor and width/height.我正在使用因子和宽度/高度计算所选视图的偏移量。 After that I create new borders (left, right, top, bottom), add an offset to the borders of the selected view and then use them instead of the original in the method之后,我创建新边框(左、右、上、下),为所选视图的边框添加偏移量,然后在方法中使用它们而不是原始边框

Important:重要的:

  • You shouldn't call super你不应该叫 super
  • The coefficient must be greater than 0.5系数必须大于 0.5

I have no solution, but still, I would like to share my observation.我没有解决方案,但我仍然想分享我的观察。 It seems like whatever value you return from getMoveThreshold() method that is below 1 does not change the default behaviour, however, values above 1 in fact does.看起来您从 getMoveThreshold() 方法返回的任何低于 1 的值都不会改变默认行为,但是实际上高于 1 的值会改变。

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

相关问题 Android:Recyclerview 与 ItemTouchHelper.Callback 在列表中的较低项目上闪烁 - Android: Recyclerview with ItemTouchHelper.Callback flickering on lower items in list 使用 ItemTouchHelper.Callback 后如何按顺序设置项目 position - How to set Item position in order after using ItemTouchHelper.Callback 使用ItemTouchHelper在RecyclerView中悬停事件 - Hover event in RecyclerView using ItemTouchHelper 在RecyclerView中禁用ItemTouchHelper滑动某些行 - Disable ItemTouchHelper Swipe for some rows in RecyclerView 使用ItemTouchHelper与RecyclerView中的锁定ViewHolder进行交互 - Interaction with locked ViewHolder in RecyclerView using ItemTouchHelper 如何从 ItemTouchHelper 更新 recyclerview 数据? - How to update recyclerview data from ItemTouchHelper? 在 recyclerview 中拖动不起作用,但滑动有效? - Dragging in recyclerview is not working but swiping is? 使用 ItemTouchHelper 从 RecyclerView 中删除项目时引发 UnsupportedOperationException - UnsupportedOperationException is thrown when removing item from RecyclerView using ItemTouchHelper 如何在RecyclerView的ItemTouchHelper中收听点击事件? - How can I listen to click events in RecyclerView's ItemTouchHelper? 在拖动/重新排序期间调整itemTouchhelper上的所有recyclerview单元格的大小 - Resize all recyclerview cells on itemTouchhelper during drag/reorder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM