Android - 滚动后无法单击RecyclerView中的项目

Kim*_*hiu 30 android android-support-library android-recyclerview android-coordinatorlayout

我刚刚升级到API 26并支持库26.0.2.但我发现RecyclerView滚动后我的项目无法点击.如果你等一下,它会起作用.但是如果你立即点击该项目,它就不会.即使RecyclerView根本没有滚动(例如已滚动到顶部).

当我降级为支持库25.4.0时,一切都会好起来的.关键的一点是,我RecyclerView是在CoordinatorLayout和具有SCROLL_FLAG_SCROLL标志上我Toolbar的AppBarLayout.如果我不使用此标志,则此问题将消失.所以我认为这是支持库26的隐藏行为变化.

我试图添加focusable="false"到CoordinatorLayout但仍然没有运气.

有没有办法禁用这种行为?因为点击两次触发点击事件真的很烦人.

 <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinateLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
            android:id="@+id/fragmentAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp"
            android:background="@null">
        <include
                android:id="@+id/dynamicActionBarHolder"
                layout="@layout/dynamic_action_bar"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/pullToRefreshMailRecycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
                android:id="@+id/mailRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

    </android.support.v4.widget.SwipeRefreshLayout>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

编辑

我认为这个问题是scrollState的RecyclerView.当它停止滚动时,它不会SCROLL_STATE_IDLE立即更改为.查看源代码RecyclerView,我发现有一个ViewFlinger控制滚动状态.当我向下滚动到顶部时,它不会setScrollState(SCROLL_STATE_IDLE)立即调用,而是等待一段时间来触发此方法.我越快,我需要等待的时间越多.就像RecyclerView仍然在后台滚动一样.因为在触摸顶部时停止滚动scroller.isFinished()后不会返回true RecyclerView.也许这是一个错误RecyclerView的时间CoordinatorLayout.

Kim*_*hiu 38

找到了一种强制滚动状态为空闲的方法.等待谷歌修复此错误.

@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;
}
Run Code Online (Sandbox Code Playgroud)

编辑

该问题已在支持库27.0.1中得到修复.

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

用户滚动后,他们无法单击RecyclerView中的项目.(AOSP问题66996774)

2017年11月17日更新

一些用户报告在支持库27.0.1中没有修复此问题.问题跟踪器在这里. https://issuetracker.google.com/issues/66996774

因此,您可以选择使用此官方解决方法. https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2

或者在这里使用这个.


Ser*_*gey 5

非常感谢您提出的问题和答案!它节省了我很多时间。抱歉,将其发布为答案。我的声誉不足,无法发表评论。

我也注意到了这个问题,但是作为一个新的Android开发人员,我没有意识到这是新的支持库中的错误。

我想建议的也是添加此检查:

if( requestCancelDisallowInterceptTouchEvent ){
    if (!canScrollVertically(-1) || !canScrollVertically(1)) {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

这将确保在实际滚动RecyclerView时,我们不会单击任何项​​目。

据我了解,这是一种预期的行为。但是,您的回答帮助了我解决这个问题。