如何实现如下图:
向右滑动(向左)
向左滑动(向右)
)
代码:
这是我尝试的代码,但无法实现与图像相同的代码。如何设计如图所示的带有背景颜色和圆角的文字?
@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) {
View itemView = viewHolder.itemView;
Paint p = new Paint();
if (dX > 0) {
c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
(float) itemView.getBottom(), p);
} else {
RectF rightButton = new RectF(itemView.getRight() , itemView.getTop(), itemView.getRight(), itemView.getBottom());
p.setColor(Color.BLACK);
c.drawRoundRect(rightButton, dX, dY, p);
drawText(mContext.getString(R.string.cancel), c, rightButton, p);
}
// Fade out the view when it is swiped out of the parent
final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setTranslationX(dX);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
private void drawText(String text, Canvas c, RectF button, Paint p) {
float textSize = 30;
p.setColor(Color.WHITE);
p.setAntiAlias(true);
p.setTextSize(textSize);
float textWidth = p.measureText(text);
c.drawText(text, button.centerX() - (textWidth / 2), button.centerY() + (textSize / 2), p);
}
最佳答案
您可以通过在 ItemTouchHelper.SimpleCallback
上设置 Recyclerview
来实现它。
你可以在这里查看这个很好的例子:
https://www.androidhive.info/2017/09/android-recyclerview-swipe-delete-undo-using-itemtouchhelper/
关于android - 带有背景颜色和文本的 Recycler View 轻扫在轻扫时不显示矩形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52671650/