Android - the item inside RecyclerView can't be clicked after scroll

穿精又带淫゛_ 提交于 2019-11-27 11:22:02

Found a way to force the scroll state to be idle. Waiting for google to fix this bug.

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    boolean requestCancelDisallowInterceptTouchEvent = getScrollState() == SCROLL_STATE_SETTLING;
    boolean consumed = super.onInterceptTouchEvent(event);
    final int action = event.getActionMasked();

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if( requestCancelDisallowInterceptTouchEvent ){
                getParent().requestDisallowInterceptTouchEvent(false);

                // only if it touched the top or the bottom. Thanks to @Sergey's answer.
                if (!canScrollVertically(-1) || !canScrollVertically(1)) {
                    // stop scroll to enable child view to get the touch event
                    stopScroll();
                    // do not consume the event
                    return false;
                }
            }
            break;
    }

    return consumed;
}

EDIT

The issue has been fixed in support library 27.0.1.

https://developer.android.com/topic/libraries/support-library/revisions.html#27-0-1

After a user scrolls, they cannot click on an item in a RecyclerView. (AOSP issue 66996774)

Updated on Nov 17, 2017

Some users reported that this problem is not fixed in support library 27.0.1. The issue tracker is here. https://issuetracker.google.com/issues/66996774

So you may choose to use this official workaround. https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2

Or use this one here.

Thank you very much for this question and your answer! It saved me a lot of time. Sorry for posting this as an answer. I don't have enough reputation to comment.

I also noticed this issue, but as a new Android developer I didn't realize it's a bug inside the new support library.

What I wanted to suggest is also adding this check:

if( requestCancelDisallowInterceptTouchEvent ){
    if (!canScrollVertically(-1) || !canScrollVertically(1)) {
        ...
    }
}

It will make sure that while the RecyclerView is actually being scrolled, we don't click on any item.

As I understand, it's an expected behaviour. However, your answer helped me with this question.

I have found a source code that solved this issue. This issue occur because of AppBarLayout's behavior (AppBarLayout.Behavior). This source code provide an extending or a customizing behavior of behavior of AppBarLayout and set it into the AppBarLayout with introduction of usage both in xml directly or java. I can only explain briefly about it because the source has license that you must read it too. Please see the solution in this link: https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!