[英]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:重要的:
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.