热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

API28中的ItemTouchHelperonChildDraw()

如何解决《API28中的ItemTouchHelperonChildDraw()》经验,为你挑选了1个好方法。

我在Android API 28上遇到一个奇怪的问题ItemTouchHelper,它在滑动时未绘制图标之一。他们是否更改了我不知道的新版本中的某些内容?

编辑图标显示在API 27中,但未显示在API 28中。

删除图标显示在两个API版本上。

在两个版本上,图标的计算位置相同。

记录删除图标

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 {
        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)
    }
}

Ben P... 5

编辑图标上的左右边界是相反的。

// 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);
    }
}

然后,我将其中之一放入布局作为活动中的唯一视图:



当我运行该应用程序时,我什么都看不到。

如果我切换此行:

icon.setBounds(getRight(), getTop(), getLeft(), getBottom());

改为:

icon.setBounds(getLeft(), getTop(), getRight(), getBottom());

然后一切都按预期工作,并且我的图标充满了整个屏幕。



1> Ben P...:

编辑图标上的左右边界是相反的。

// 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);
    }
}

然后,我将其中之一放入布局作为活动中的唯一视图:



当我运行该应用程序时,我什么都看不到。

如果我切换此行:

icon.setBounds(getRight(), getTop(), getLeft(), getBottom());

改为:

icon.setBounds(getLeft(), getTop(), getRight(), getBottom());

然后一切都按预期工作,并且我的图标充满了整个屏幕。


推荐阅读
author-avatar
竹林映uj
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有