事情是我想在将项目拖到RecyclerView中的同时对其进行标高。
根据文档的说明,我应该在ItemTouchHelper.Callback#onChildDraw中自定义拖动 View 的行为,因为它说
然后,我应该在ItemTouchHelper.Callback#clearView中清除我的自定义设置,因为它说
但是问题是onChildDraw
总是在clearView
之后再被调用一次。
因此,即使我清除了clearView
中的自定义行为,仍然会在onChildDraw
中对其进行重新调用。那么clearView
的目的是什么?
这是我的代码:
class MyItemTouchCallback extends ItemTouchHelper.Callback {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.UP | ItemTouchHelper.DOWN);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
// Set elevation for dragged itemView
ViewCompat.setElevation(viewHolder.itemView, getResources().getDimension(R.dimen.common_elevation));
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
// Clear elevation for dragged itemView
ViewCompat.setElevation(viewHolder.itemView, 0);
}
}
最佳答案
当然,我们可以通过技巧解决此问题,这很容易理解。记住在清除拖动的itemView时使用 bool 变量viewBeingCleared
,然后我们可以决定是否调用onChildDraw
中的自定义代码。
但是,这不是最佳解决方案。我们最好知道为什么使用,然后我们将知道如何使用,不是吗?
特技解决方案:
class MyItemTouchCallback extends ItemTouchHelper.Callback {
boolean viewBeingCleared;
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.UP | ItemTouchHelper.DOWN);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (viewBeingCleared) {
viewBeingCleared = false;
} else {
ViewCompat.setElevation(viewHolder.itemView, getResources().getDimension(R.dimen.common_elevation));
}
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
ViewCompat.setElevation(viewHolder.itemView, 0);
viewBeingCleared = true;
}
}