Android 右击时的RecyclerView添加图标
我已经将touchCallback添加到我的RecyclerView中,并且在使用相对图标滑动项目时添加了背景色 问题是我在向左滑动时实现了图标的定位,但当项目向右滑动时,我无法正确设置它 我只能看到部分图标,而我希望它显示在左扫 其实都是这样的: 我的touchCallback函数如下所示:Android 右击时的RecyclerView添加图标,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我已经将touchCallback添加到我的RecyclerView中,并且在使用相对图标滑动项目时添加了背景色 问题是我在向左滑动时实现了图标的定位,但当项目向右滑动时,我无法正确设置它 我只能看到部分图标,而我希望它显示在左扫 其实都是这样的: 我的touchCallback函数如下所示: private fun initSwipe(recycler: RecyclerView) { val simpleItemTouchCallback = object : ItemTouchHe
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)
图标的左侧应位于视图的左边缘加上边距大小:
val deleteIconLeft = itemView.left + deleteIconMargin
你没有计算出正确的一面。右侧应为左侧加上宽度:
val deleteIconRight = deleteIconLeft + instrinsicWidth!!
然后,在设置边界时,需要使用右侧,而不是边距:
editIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
我创建了一个库,它在两个滑动方向上添加图标和可选文本。您可以将其添加到
build.gradle
。以下是指向文档的链接:
我创建了一个库,它在两个滑动方向上添加图标和可选文本。您可以将其添加到
build.gradle
。以下是指向文档的链接:
这里看起来“有点”拉长了。我没有看你剩下的代码。刚刚看到了不合适的负号。我更新了它,对它进行了更全面的检查。我多次使用该值,但从未尝试设置
iconRight=iconLeft+instrinsicWidth代码>这里看起来“有点”拉长了。我没有看你剩下的代码。刚刚看到了不合适的负号。我更新了它,对它进行了更全面的检查。我多次使用该值,但从未尝试设置iconRight=iconLeft+instrinsicWidth代码>