Android ItemTouchHelper部分滑动

时间:2019-01-14 07:38:14

标签: java android android-recyclerview itemtouchhelper

从尾部滑动时,我可以显示红色背景和一个删除图标。我正在尝试实现部分滑动功能,其中在部分滑动时,滑动会在删除图标之前停止。我尝试遵循这些solutions。但是,它对我不起作用。请指出正确的方向。

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);
    }
}

}

1 个答案:

答案 0 :(得分:1)

在此行中将dx参数更改为dx / 3或dx / 5:

super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

因为dx参数值介于(0表示不刷卡)(1表示完全刷卡)