【发布时间】: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