【问题标题】:RecyclerView add icon on right swipeRecyclerView 在右侧滑动添加图标
【发布时间】:2021-01-20 13:57:17
【问题描述】:

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

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

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

其实都是这样的:

而我的 touchCallback 函数如下:

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)
}

【问题讨论】:

    标签: android kotlin android-recyclerview


    【解决方案1】:

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

    val deleteIconLeft = itemView.left + deleteIconMargin
    

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

    val deleteIconRight = deleteIconLeft + instrinsicWidth!!
    

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

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

    【讨论】:

    • 这里看起来“有点”被拉长了photo
    • 糟糕。我没有看你的其余代码。刚刚看到不合适的减号。我更新了对它的更完整的评论。
    • 多次使用该值,但从未尝试设置 iconRight = iconLeft + instrinsicWidth!!
    【解决方案2】:

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

    【讨论】:

    • 如何在java中使用
    • @ElfnanSherif 您只需将库添加到您的 build.gradle 文件中,同步它,然后按照有关如何在代码中以与 java 中相同的方式调用库的指南进行操作。有关如何使用该库的所有信息都可以在此地址获得:github.com/kevingermainbusiness/ItemDecorator
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多