私はこのようなものを作成しました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% だけを移動させたいのです。前進するためのヒントを教えてください..