[英]Android RecyclerView Swipe to delete with Icon only drawed on first element
一切都很好,但是一件事情並沒有真正起作用。 如您在圖像中看到的,刪除圖標僅呈現在回收者視圖列表的第一個元素上。
這是我的ItemTouchHelper類的代碼:
class ItemSwipeCallback(val context: Context) : ItemTouchHelper.Callback() {
private val listeners = ArrayList<OnItemSwipe>()
private val paint = Paint()
val theme = context.themeId
val icon = ContextCompat.getDrawable(context, R.drawable.ic_delete_filled_white_24dp)!!
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder): Boolean {
return true
}
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
val direction = context.sharedPreferences.getInt(Preferences.SWIPE_DIRECTION, Preferences.SWIPE_VALUE_RIGHT)
return when (direction) {
Preferences.SWIPE_VALUE_RIGHT -> makeMovementFlags(0, ItemTouchHelper.RIGHT)
Preferences.SWIPE_VALUE_LEFT -> makeMovementFlags(0, ItemTouchHelper.LEFT)
else -> makeMovementFlags(0, ItemTouchHelper.RIGHT)
}
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listeners.forEach { it.onSwiped(viewHolder.layoutPosition, direction) }
}
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
if (dX != 0f && isCurrentlyActive) {
val itemView = viewHolder.itemView
paint.color = Color.parseColor("#D32F2F")
val top = (itemView.height - icon.intrinsicHeight) / 2
val left = itemView.width - icon.intrinsicWidth - top
if (theme == Preferences.THEME_VALUE_DARK) {
icon.setTint(Color.BLACK)
} else {
icon.setTint(Color.WHITE)
}
if (dX < 0) {
val background = RectF(itemView.right.toFloat() + dX, itemView.top.toFloat(),
itemView.right.toFloat(), itemView.bottom.toFloat())
c.drawRect(background, paint)
icon.setBounds(left, top, left + icon.intrinsicWidth, top + icon.intrinsicHeight)
} else {
val background = RectF(itemView.left.toFloat() + dX, itemView.top.toFloat(),
itemView.left.toFloat(), itemView.bottom.toFloat())
c.drawRect(background, paint)
icon.setBounds(top, top, top + icon.intrinsicWidth, top + icon.intrinsicHeight)
}
icon.draw(c)
}
}
fun addOnItemSwipeListener(onItemSwipe: OnItemSwipe) {
listeners.add(onItemSwipe)
}
}
也許加載到課程開頭的圖標只能使用一次? 我已經嘗試將其轉換為位圖並使用它。 我也嘗試在onChildDraw函數中加載它。
解決方案太簡單了。 我總是使用itemView.height
而不是itemView.top
。
畫布包含所有項目。 並非每個項目都有自己的畫布。 所以我也必須添加以上項目的高度。
工作代碼如下所示:
val top = itemView.top + (itemView.height - intrinsicHeight) / 2
val left = itemView.width - intrinsicWidth - (itemView.height - intrinsicHeight) / 2
val right = left + intrinsicHeight
val bottom = top + intrinsicHeight
if (dX < 0) {
background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
icon.setBounds(left, top, right, bottom)
} else if (dX > 0) {
background.setBounds(itemView.left + dX.toInt(), itemView.top, itemView.left, itemView.bottom)
icon.setBounds(top, top, top, bottom)
}
background.draw(c)
icon.draw(c)
您是否檢查了此參數的值:isCurrentlyActive?
我猜圖像創建(圖標)中沒有錯誤。 因為圖像是第一次成功創建的。 因此,問題出在循環中。
if (dX < 0) {...}else{...}
在這里,無論dX的值如何,圖像都會添加到子級(行)中。
if (dX != 0f && isCurrentlyActive)
這是代碼中唯一的檢查點。 從技術上講,如果isCurrentlyActive布爾值為false,則將跳過整個塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.