Dan*_*hle 3 android kotlin android-recyclerview
一切都运转良好,但有一件事并没有真正发挥作用。删除图标仅呈现在回收器视图列表的第一个元素上,如图所示。
这是我的 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)
}
}
Run Code Online (Sandbox Code Playgroud)
也许加载在类头部的图标只能使用一次?我已经尝试将其转换为位图并使用它。我还尝试将其加载到 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6518 次 |
| 最近记录: |