如何知道用户何时完成移动 RecyclerView 项目?
How to know when user is done moving a RecyclerView item?
我需要在用户放下物品时执行某些操作。 ItemTouchHelper 似乎只有对 onMove 和 onMoved 的调用,无论用户是否真的完成了项目的移动,它们都会发生。
我如何知道用户何时完成移动项目?也就是说,他们松开了手指并将物品放回了 recyclerView?
您可以覆盖 onSelectedChanged()
ItemTouchHelper.Callback
class 实现的回调,并使用可以是 (ACTION_STATE_DRAG
, ACTION_STATE_SWIPE
, & ACTION_STATE_IDLE
).
ACTION_STATE_IDLE
是您要查找的内容,它将在 swipe/drag 操作结束并且用户将手指离开屏幕时触发
您可以使用布尔值来判断它是滑动还是拖动,如下所示。
private ItemTouchHelper createHelperCallback() {
return new ItemTouchHelper(new ItemTouchHelper.Callback() {
final boolean[] isSwiped = {false};
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return 0;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
switch (actionState) {
case ItemTouchHelper.ACTION_STATE_DRAG:
// the user is dragging an item and didn't lift their finger off yet
isSwiped[0] = false;
break;
case ItemTouchHelper.ACTION_STATE_SWIPE:
// the user is swiping an item and didn't lift their finger off yet
isSwiped[0] = true;
break;
case ItemTouchHelper.ACTION_STATE_IDLE:
// the user just dropped the item (after dragging it), and lift their finger off.
if (isSwiped[0]) // The user used onSwiped()
Toast.makeText(MainActivity.this, "Swiping is over", Toast.LENGTH_SHORT).show();
else // The user used onMove()
Toast.makeText(MainActivity.this, "Dragging & Dropping are over", Toast.LENGTH_SHORT).show();
}
}
});
}
然后在你的 RecyclerView 上使用它
createHelperCallback().attachToRecyclerView(recyclerView);
预览
简单。只需覆盖 onClearView()
:
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { //called when you dropped the item
super.clearView(recyclerView, viewHolder);
Toast.makeText(recyclerView.getContext(), "Item dropped on position: " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
你也可以覆盖getAnimationDuration()
,它在onSelectedChanged()
之前调用(当用户的手指向上时)但是你不能在这个函数中访问viewHolder
。
我需要在用户放下物品时执行某些操作。 ItemTouchHelper 似乎只有对 onMove 和 onMoved 的调用,无论用户是否真的完成了项目的移动,它们都会发生。 我如何知道用户何时完成移动项目?也就是说,他们松开了手指并将物品放回了 recyclerView?
您可以覆盖 onSelectedChanged()
ItemTouchHelper.Callback
class 实现的回调,并使用可以是 (ACTION_STATE_DRAG
, ACTION_STATE_SWIPE
, & ACTION_STATE_IDLE
).
ACTION_STATE_IDLE
是您要查找的内容,它将在 swipe/drag 操作结束并且用户将手指离开屏幕时触发
您可以使用布尔值来判断它是滑动还是拖动,如下所示。
private ItemTouchHelper createHelperCallback() {
return new ItemTouchHelper(new ItemTouchHelper.Callback() {
final boolean[] isSwiped = {false};
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return 0;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
switch (actionState) {
case ItemTouchHelper.ACTION_STATE_DRAG:
// the user is dragging an item and didn't lift their finger off yet
isSwiped[0] = false;
break;
case ItemTouchHelper.ACTION_STATE_SWIPE:
// the user is swiping an item and didn't lift their finger off yet
isSwiped[0] = true;
break;
case ItemTouchHelper.ACTION_STATE_IDLE:
// the user just dropped the item (after dragging it), and lift their finger off.
if (isSwiped[0]) // The user used onSwiped()
Toast.makeText(MainActivity.this, "Swiping is over", Toast.LENGTH_SHORT).show();
else // The user used onMove()
Toast.makeText(MainActivity.this, "Dragging & Dropping are over", Toast.LENGTH_SHORT).show();
}
}
});
}
然后在你的 RecyclerView 上使用它
createHelperCallback().attachToRecyclerView(recyclerView);
预览
简单。只需覆盖 onClearView()
:
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { //called when you dropped the item
super.clearView(recyclerView, viewHolder);
Toast.makeText(recyclerView.getContext(), "Item dropped on position: " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
你也可以覆盖getAnimationDuration()
,它在onSelectedChanged()
之前调用(当用户的手指向上时)但是你不能在这个函数中访问viewHolder
。