android - OnClickListener not working for first click in recyclerview
Make sure you have both focusableInTouchMode
& focusable
disabled on the button. The first click will get the focus and the second click executes the onClickListener.
.
After scroll, recycler view items are not clickable and the issue is still open https://issuetracker.google.com/issues/66996774
Found a way to force click if the scroll state is still SCROLL_STATE_SETTLING
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
class WrapperRecyclerView(context: Context, attributeSet: AttributeSet?, defStyle: Int) :
RecyclerView(context, attributeSet, defStyle) {
constructor(context: Context) : this(context, null, 0)
constructor(context: Context, attributeSet: AttributeSet) : this(context, attributeSet, 0)
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
val eventConsumed = super.onInterceptTouchEvent(event)
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> if (scrollState == SCROLL_STATE_SETTLING) {
parent.requestDisallowInterceptTouchEvent(false)
// only if it touched the top or the bottom.
if (!canScrollVertically(-1) || !canScrollVertically(1)) {
// stop scroll to enable child view to get the touch event
stopScroll()
// do not consume the event
return false
}
}
}
return eventConsumed
}
}