1

我创造了一个RecyclerView ItemTouchHelper这样的 -

public class MyItemTouchHelper extends android.support.v7.widget.helper.ItemTouchHelper.SimpleCallback {
private final Paint p;
Context mContext;

@Override
public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
    return viewHolder.itemView.getWidth()/3;
}

public MyItemTouchHelper(Context context) {
    super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
    mContext = context;
    p = new Paint();

}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return false;
}


@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    int position = viewHolder.getAdapterPosition();

    if (direction == ItemTouchHelper.LEFT) {
    } else {
    }
}


@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder
        viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    Bitmap icon;
    String text = "Like it, remind me!";

    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        View itemView = viewHolder.itemView;

        float height = (float) itemView.getBottom() - (float) itemView.getTop();
        float width = height / 10;
        if (dX > 0) {
            p.setColor(Color.parseColor("#388E3C"));

            RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX/3, (float) itemView.getBottom());
            c.drawRect(background, p);

            Rect r = new Rect();
            p.getTextBounds(text, 0, text.length(), r);
            float yPos = height/2;

            yPos += (Math.abs(r.height()))/2;
            int xPos = (c.getWidth() / 2);
            p.setTextAlign(Paint.Align.CENTER);
            c.drawText(text,(float)xPos  , yPos, p);
        } else {
            p.setColor(Color.parseColor("#D32F2F"));

            RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
            c.drawRect(background, p);


            RectF icon_dest = new RectF((float) itemView.getRight() - 2 * width, (float) itemView.getTop() + width, (float) itemView.getRight() - width, (float) itemView.getBottom() - width);
        }

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

}

}

但这是将整行从屏幕上滑下来,我只希望 25% 的行替换。让我知道任何前进的提示..

4

1 回答 1

2

在将其传递给方法之前,覆盖onChildDraw并除以dX大于 1 的因子。super.onChildDraw对于通过滑动移动的视图持有者dX,它将在屏幕上绘制为已移动dX / factor

于 2017-08-25T18:29:02.417 回答