繁体   English   中英

RecyclerView 在右侧滑动添加图标

[英]RecyclerView add icon on right swipe

我已将 touchCallback 添加到我的 RecyclerView 中,并且当使用相对图标滑动项目时,我正在添加背景颜色。

问题是我在左滑动时实现了图标的定位,但是当项目向右滑动时我无法正确设置它。

我只能看到部分图标,而我希望它像左滑动一样显示..

其实一切看起来都是这样的: 在此处输入图像描述

我的 touchCallback function 如下:

private fun initSwipe(recycler: RecyclerView) {
    val simpleItemTouchCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
        private val clearPaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) }
        override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
            return false
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            val position = viewHolder.adapterPosition

            if (direction == ItemTouchHelper.LEFT) {

                Toast.makeText(activity, position.toString(), Toast.LENGTH_LONG).show()
                recycler.adapter?.notifyItemChanged(position)
            } else {

                Toast.makeText(activity, position.toString(), Toast.LENGTH_LONG).show()
                recycler.adapter?.notifyItemChanged(position)
            }


        }

        override fun onChildDraw(
            c: Canvas,
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            dX: Float,
            dY: Float,
            actionState: Int,
            isCurrentlyActive: Boolean
        ) {
            val background = ColorDrawable()
            val itemView = viewHolder.itemView
            val itemHeight = itemView.bottom - itemView.top
            val isCanceled = dX == 0f && !isCurrentlyActive

            if (isCanceled) {
                clearCanvas(c, itemView.right + dX, itemView.top.toFloat(), itemView.right.toFloat(), itemView.bottom.toFloat())
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
                return
            }

            if (dX > 0) {
                val editIcon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_edit)
                val intrinsicWidth = editIcon?.intrinsicWidth
                val intrinsicHeigh = editIcon?.intrinsicHeight
                // Draw the green background
                val backgroundColor = Color.parseColor("#32a852")
                background.color = backgroundColor
                background.setBounds(itemView.left, itemView.top, dX.toInt() + 10, itemView.bottom)
                background.draw(c)

                // Calculate position of delete icon
                val deleteIconTop = itemView.top + (itemHeight - intrinsicHeigh!!) / 2
                val deleteIconMargin = (itemHeight - intrinsicHeigh) / 2
                val deleteIconLeft = itemView.left - deleteIconMargin - intrinsicWidth!!
                val deleteIconBottom = deleteIconTop + intrinsicHeigh

                // Draw the delete icon
                editIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconMargin, deleteIconBottom)
                editIcon.draw(c)
            }else {
                val deleteIcon = ContextCompat.getDrawable(requireContext(), R.drawable.ic_delete)
                val intrinsicWidth = deleteIcon?.intrinsicWidth
                val intrinsicHeigh = deleteIcon?.intrinsicHeight
                // Draw the delete background
                val backgroundColor = Color.parseColor("#f44336")
                background.color = backgroundColor
                background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
                background.draw(c)

                // Calculate position of delete icon
                val deleteIconTop = itemView.top + (itemHeight - intrinsicHeigh!!) / 2
                val deleteIconMargin = (itemHeight - intrinsicHeigh) / 2
                val deleteIconLeft = itemView.right - deleteIconMargin - intrinsicWidth!!
                val deleteIconRight = itemView.right - deleteIconMargin
                val deleteIconBottom = deleteIconTop + intrinsicHeigh

                // Draw the delete icon
                deleteIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
                deleteIcon.draw(c)
            }

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


        private fun clearCanvas(c: Canvas?, left: Float, top: Float, right: Float, bottom: Float) {
            c?.drawRect(left, top, right, bottom, clearPaint)
        }
    }


    val itemTouchHelper = ItemTouchHelper(simpleItemTouchCallback)
    itemTouchHelper.attachToRecyclerView(recycler)
}

图标的左侧应该在视图的左边缘加上边距大小:

val deleteIconLeft = itemView.left + deleteIconMargin

你没有计算右边。 右边应该是左边加上宽度:

val deleteIconRight = deleteIconLeft + instrinsicWidth!!

然后你需要在设置边界时使用右侧,而不是边距:

editIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)

我为该确切用途创建了一个库,它在两个滑动方向上都添加了图标和可选的文本。 您可以将其添加到您的build.gradle中。 这是文档的链接: https://github.com/kevingermainbusiness/ItemDecorator

在此处输入图像描述 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM