我在Android API 28上遇到一个奇怪的问题,ItemTouchHelper
在滑动时不会绘制图标之一。他们是否更改了我不知道的新版本中的某些内容?
两个版本中图标的计算位置相同。
登录删除图标
API 27: D/Position: Left: 938 Top: 100 Right: 1001 Bottom: 163
API 28: D/Position: Left: 938 Top: 100 Right: 1001 Bottom: 163
登录以编辑图标
API 27: D/Position: Left: 142 Top: 100 Right: 79 Bottom: 163
API 28: D/Position: Left: 142 Top: 100 Right: 79 Bottom: 163
ItemTouchHelper
abstract class ImageGroupTouchCallback(context: Context) : ItemTouchHelper.Callback() {
[...] -> unimportant code removed
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
[...] -> unimportant stuff
// Calculate position of the icon
val iconMargin = (itemHeight - intrinsicHeight) / 2
val iconTop = itemView.top + (itemHeight - intrinsicHeight) / 2
val iconBottom = iconTop + intrinsicHeight
val (iconLeft, iconRight) = getIconPositionHorizontal(itemView, iconMargin, dX)
Log.d("Position", "Left: $iconLeft Top: $iconTop Right: $iconRight Bottom: $iconBottom")
// swiping from left to right
if (dX > 0) {
background.setBounds(itemView.left, itemView.top, itemView.left + dX.toInt(), itemView.bottom)
background.color = Color.parseColor("#3cca59")
background.draw(c)
// Draw the delete icon
editIcon!!.setBounds(iconLeft, iconTop, iconRight, iconBottom)
editIcon.draw(c)
}
// swiping from right to left
else if (dX < 0) {
background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
background.color = Color.parseColor("#f44336")
background.draw(c)
// Draw the delete icon
deleteIcon!!.setBounds(iconLeft, iconTop, iconRight, iconBottom)
deleteIcon.draw(c)
}
}
private fun getIconPositionHorizontal(itemView: View, iconMargin: Int, dX: Float): Pair<Int, Int> {
val iconLeft: Int
val iconRight: Int
// swiping from left to right
if (dX > 0) {
iconLeft = itemView.left + iconMargin + intrinsicWidth
iconRight = itemView.left + iconMargin
} else {
iconLeft = itemView.right - iconMargin - intrinsicWidth
iconRight = itemView.right - iconMargin
}
return Pair(iconLeft, iconRight)
}
}
答案 0 :(得分:4)
您的编辑图标的左右边界是相反的。
// swiping from left to right if (dX > 0) { iconLeft = itemView.left + iconMargin + intrinsicWidth iconRight = itemView.left + iconMargin }
这将返回一对坐标,其中图标左侧的值大于其右侧的值。另一方面,您的删除图标的坐标设置正确。
用上面的代码替换上面的代码:
// swiping from left to right
if (dX > 0) {
iconLeft = itemView.left + iconMargin
iconRight = itemView.left + iconMargin + intrinsicWidth
}
为重现此问题,我创建了一个非常简单的视图类:
public class MyView extends View {
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Drawable icon = ContextCompat.getDrawable(getContext(), R.drawable.ic_bookmark_black_24dp);
icon.setBounds(getRight(), getTop(), getLeft(), getBottom());
icon.draw(canvas);
}
}
然后我只是将其中之一粘贴到布局中作为活动中的唯一视图:
<?xml version="1.0" encoding="utf-8"?>
<com.example.stackoverflow.MyView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
运行该应用程序时,我什么都看不到。
如果我切换此行:
icon.setBounds(getRight(), getTop(), getLeft(), getBottom());
改为:
icon.setBounds(getLeft(), getTop(), getRight(), getBottom());
然后一切都按预期工作,并且我的图标充满了屏幕。