I have a recycler view with ItemTouchHelper. It allows dragging the items.
I want to limit the dragging to the bounds of recycler view - ie you can't just drag the view outside the container, so that it disappears.
I tried checking the absolute coordinates like this:
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
recyclerView.getLocationOnScreen(pos);
int rvY = pos[1];
viewHolder.itemView.getLocationOnScreen(pos);
int vhY = pos[1];
if (rvY > vhY || rvY + recyclerView.getHeight() < vhY + viewHolder.itemView.getHeight()) {
return;
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
...
}
but then I run into kinda rendering concurency - if I move the view slowly, it will stop moving when going out of bounds, but if I move faster - then it anyway leaves the recycler view bounds.
Any ideas / approaches?
The dY and dX value must be clipped to the RecyclerView
's bounds:
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
val clippedDx = clip(recyclerView.width, viewHolder.itemView.left, viewHolder.itemView.right, dX)
val clippedDy = clip(recyclerView.height, viewHolder.itemView.top, viewHolder.itemView.bottom, dY)
super.onChildDraw(c, recyclerView, viewHolder, clippedDx, clippedDy, actionState, isCurrentlyActive)
}
private fun clip(size: Int, start: Int, end: Int, delta: Float): Float {
val newStart = start + delta
val newEnd = end + delta
val oobStart = 0 - newStart
val oobEnd = newEnd - size
return when {
oobStart > 0 -> delta + oobStart
oobEnd > 0 -> delta - oobEnd
else -> delta
}
}
I adjusted user1185087's answer so that instead of teleporting your item, it simply won't handle it if the drag does go out.
@Override
public void onChildDraw(@NotNull Canvas c, @NotNull RecyclerView recyclerView,
@NotNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
float topY = viewHolder.itemView.getTop() + dY;
float bottomY = topY + viewHolder.itemView.getHeight();
// Only redraw child if it is inbounds of view
if (topY > 0 && bottomY < recyclerView.getHeight()){
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
I have the same problem. All answers help me find the better way without lags. :)
If use only dY it will create lags. So I add position determination for itemView inside onChildDraw .
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
var edgeY = dY
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
val lastPosition = (recyclerView.adapter?.itemCount ?: 0) - 1
val view = viewHolder.itemView
val pieceHeight = view.height / PIECE_RATIO
val topEdge = recyclerView.top - pieceHeight
val bottomEdge = recyclerView.height - view.bottom + pieceHeight
if (movePosition == 0 && dY < topEdge) {
edgeY = topEdge
} else if (movePosition == lastPosition && dY > bottomEdge) {
edgeY = bottomEdge
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, edgeY, actionState, isCurrentlyActive)
}
Code for this:
/**
* Variable need for best performance and more productive, because
* get [RecyclerView.ViewHolder.getAdapterPosition] inside [onChildDraw]
* is hard calculating operation
*/
protected var movePosition = RecyclerView.NO_POSITION
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
/**
* Get position on drag start
*/
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
movePosition = viewHolder?.adapterPosition ?: RecyclerView.NO_POSITION
}
}
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)
/**
* Clear position on drag end
*/
movePosition = RecyclerView.NO_POSITION
}
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder): Boolean {
/**
* Get position on drag
*/
movePosition = target.adapterPosition
return false
}
Child classes can use movePosition like this:
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder): Boolean {
super.onMove(recyclerView, viewHolder, target)
return callback.onTouchMove(viewHolder.adapterPosition, movePosition)
}
All source code and samples you can find in link: github project
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.