Tuesday, 2 October 2018

ItemTouchHelper onChildDraw() in API 28

I have a strange problem on Android API 28 with the ItemTouchHelper where it doesn't draw one of the icons on swiping. Did they change something in the new version that I'm not aware of?

enter image description here Edit icon is shown in API 27 but not on API 28.

enter image description here Delete icon is shown on both API versions.

The calculated position of the icons is the same on both versions.

Log for delete icon

API 27: D/Position: Left: 938 Top: 100 Right: 1001 Bottom: 163
API 28: D/Position: Left: 938 Top: 100 Right: 1001 Bottom: 163

Log for edit icon

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

        [...] -> calculate position of the icon and other unimportant stuff

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



from ItemTouchHelper onChildDraw() in API 28

No comments:

Post a Comment