android – RecyclerView ItemTouchHelper.Callback draw inflated TextView-ThrowExceptions

Exception or error:

I am trying to do gmail like swipe action. Also i want to gain more experience with ItemTouchHelper.Callback, so i do not want to use another ready to use solution. My problem is my inflated text view does not respect its bounds. Even if i set left and top manually with TextView.layout(l, t, r, b) it is always drawn left & top corner of the recycler view. But if i try to draw a drawable after Drawable.setBounds method, it is drawn correctly. What is the case ?

enter image description here

class SwipeTouchHelper : ItemTouchHelper.Callback() {

    private lateinit var textView: TextView

    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        return makeMovementFlags(0, ItemTouchHelper.START or ItemTouchHelper.END)

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        return false

    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        if (!::textView.isInitialized) {
            textView = recyclerView.inflate(R.layout.simple_textview) as TextView

        val itemView = viewHolder.itemView

        with(itemView) {
            val widthSpec = makeMeasureSpec(width, View.MeasureSpec.EXACTLY)
            val heightSpec = makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
            textView.measure(widthSpec, heightSpec)
            textView.layout(left, top, left + textView.measuredWidth, top + textView.measuredHeight)


        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)


    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {


How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *