当我们从末端滑动时,我可以显示红色背景和删除图标。我正在尝试实现部分滑动功能,在部分滑动时,滑动在删除图标之前停止。我尝试遵循这些解决方案。但是,它对我不起作用。请指出我正确的方向。
public class PriorityItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperListener mAdapter;
private Context context;
public PriorityItemTouchHelperCallback(
ItemTouchHelperListener adapter, Context context) {
mAdapter = adapter;
this.context = context;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = ItemTouchHelper.START;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// Get RecyclerView item from the ViewHolder
View itemView = viewHolder.itemView;
float height = (float) itemView.getBottom() - (float) itemView.getTop();
float width = height / 3;
Paint p = new Paint();
p.setColor(Color.RED);
if (dX > 0) {
c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
(float) itemView.getBottom(), p);
} else {
p.setColor(Color.RED);
RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
c.drawRect(background, p);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.round_delete_white_24);
RectF icon_dest = new RectF(
(float) itemView.getRight() - 2 * width + width / 2,
(float) itemView.getTop() + width + width / 8,
(float) itemView.getRight() - width / 2,
(float) itemView.getBottom() - width - width / 8);
c.drawBitmap(icon, null, icon_dest, p);
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
}