Adding a colored background with text/icon under swiped row when using Android's RecyclerView

前端 未结 7 582
遥遥无期
遥遥无期 2020-12-02 07:37

EDIT: The real problem was that my LinearLayout was wrapped in another layout, which caused the incorrect behavior. The accepted answer by Sanvywell has

7条回答
  •  日久生厌
    2020-12-02 08:04

    HappyKatz solution has a tricky bug. Is there any reason for drawing bitmap when dX==0?? In some cases this causes permanent icon visibility above list item. Also icons become visible above list item when you just touch list item and dX==1. To fix these:

            if (dX > rectOffset) {
                c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
                        (float) itemView.getBottom(), leftPaint);
                if (dX > iconOffset) {
                    c.drawBitmap(leftBitmap,
                            (float) itemView.getLeft() + padding,
                            (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - leftBitmap.getHeight()) / 2,
                            leftPaint);
                }
            } else if (dX < -rectOffset) {
                c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(),
                        (float) itemView.getRight(), (float) itemView.getBottom(), rightPaint);
                if (dX < -iconOffset) {
                    c.drawBitmap(rightBitmap,
                            (float) itemView.getRight() - padding - rightBitmap.getWidth(),
                            (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - rightBitmap.getHeight()) / 2,
                            rightPaint);
                }
            }
    

提交回复
热议问题