RecyclerView swipeToDismiss仅使用删除按钮刷新部分行

时间:2016-06-15 05:15:42

标签: android android-recyclerview

我想做类似下面的事情

enter image description here

其中应该刷新recyclerView行(仅部分)并在行的右侧显示删除按钮。

我搜索了这个例子。一切都像gmail应用程序刷卡完全滑动以解除显示存档。但是我想要一些类似于附加图像的东西,其中行只能部分滑动,而行的其余部分应该包含删除按钮的自定义视图。

有人可以举个例子吗?

感谢。

3 个答案:

答案 0 :(得分:0)

我通过在onChildDraw()

中添加以下代码来解决这个问题
if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {

                    viewHolder?.itemView?.translationX = dX / 5
                    val p = Paint()
                    p.setColor(Color.parseColor("#D32F2F"));
                    val background = RectF(viewHolder?.itemView?.getRight()?.toFloat()?.plus(dX/5) ?: 0.0f, viewHolder?.itemView?.getTop()?.toFloat() ?: 0.0f, viewHolder?.itemView?.getRight()?.toFloat() ?: 0.0f, viewHolder?.itemView?.getBottom()?.toFloat() ?: 0.0f);
                    c?.drawRect(background,p);
                    val bitmap = BitmapFactory.decodeResource(context.resources,R.drawable.ic_delete_black_24dp)
                    val background_dest = RectF(viewHolder?.itemView?.getRight()?.toFloat()?.plus(dX/5) ?: 0.0f, viewHolder?.itemView?.getTop()?.toFloat()?.div(2) ?: 0.0f, viewHolder?.itemView?.getRight()?.toFloat() ?: 0.0f, viewHolder?.itemView?.getBottom()?.toFloat() ?: 0.0f);
                    c?.drawBitmap(bitmap,null,background_dest,p)

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

答案 1 :(得分:0)

        @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;                    
                if(dX < 0) {
                    itemView.setTranslationX(dX / 5);
                    p.setColor(ContextCompat.getColor(getContext(), R.color.cherryRed));
                    RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
                    c.drawRect(background, p);
                    //draw text or icon as you want
                }
            } else {
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
        }
    };

答案 2 :(得分:0)

private const val buttonWidth = 300f
private var newDx = 0f

private fun stopSwipeInXDistance(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        newDx = dX //Important. We use this one in super, instead of regular dX
        val sign = sign(newDx)
        val limit = buttonWidth * sign

            when (sign) {
                -1f -> { //Swipe to left. button is on right side
                    if (newDx <= limit) {
                        newDx = limit
                    }
                }
                0f, 1f -> {
                    if (newDx >= limit) {
                        newDx = limit
                    }
                }
            }

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

并在 onChildDraw 中使用它而不是:

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