Search code examples
androidandroid-recyclerviewandroid-motionlayout

MotionLayout with RecyclerView click events only works after 2nd click


I am following this tutorial and source code to implement Collapsing Toolbar by using Motion Layout. When you do "fast scroll" on recyclerView and then do click event on any item of the recyclerView, this event only works after you the 2nd click. Or you have to wait 2-3 second then do the click event works. Otherwise click event can not be dispatch to the recyclerView. (Assuming it waits to finish animation on MotionLayout). How can we fix this issue?

Anyone can simulate the issue by adding below code to the adapter of the article's source code. And do fast scroll.

class ViewHolder(
        view: View,
        private val textView: TextView = view.findViewById(android.R.id.text1)
) : RecyclerView.ViewHolder(view) {


    var text: CharSequence
        get() = textView.text
        set(value) {
            textView.text = value
        }

    init {
        view.setOnClickListener { showMessage(it) }
    }

    private fun showMessage(view: View) {
        Toast.makeText(view.context, "OnClickListener: item ${adapterPosition + 1}", Toast.LENGTH_SHORT).show()
    }

}

Solution

  • Update ConstraintLayout to version 2.0.0-beta2

    https://issuetracker.google.com/issues/128914828