[英]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.