当他停止拖放我的RecyclerView时(当他放弃所选项目时),我需要听取用户的意见。
我可以通过我的ItemTouchHelper获取此信息吗?
感谢您的帮助
Mark:目前我只是在用户仍然移动项目时:)
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType()) {
return false;
}
// Notify the adapter of the move
mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
return true;
}
答案 0 :(得分:19)
<强>已更新强>
首先,您可以通过实施canDropOver
@Override
public boolean canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
return current.getItemViewType() == getItemViewType();
}
更新要使用的适配器onMove
在拖动操作期间可以调用多次
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
adapter.moveItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
要检测与元素的交互何时超过实现clearView
,这适用于任何操作类型(拖动或滑动),即使它成功(项目移动或滑动)或取消(项目未移动或刷卡)
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
// Action finished
}
答案 1 :(得分:0)
您可以在实现 ItemTouchHelper.Callback()的过程中覆盖 onSelectedChange ,例如:
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
when (actionState) {
ItemTouchHelper.ACTION_STATE_DRAG ->
Log.d("DragTest","Start to drag: $actionState")
ItemTouchHelper.ACTION_STATE_SWIPE ->
Log.d("DragTest","Start to swipe: $actionState")
ItemTouchHelper.ACTION_STATE_IDLE -> {
Log.d("DragTest","End action: $actionState")
}
}
}