【问题标题】:Better way to fix "Touch Intercepted after fling"修复“投掷后触摸被拦截”的更好方法
【发布时间】:2017-10-12 21:03:30
【问题描述】:

我遇到了与这个问题 here 相同的问题。 问题是,在 fling 发生后,RecyclerView 子级不会获得触摸事件。只有当 recyclerview 到达顶部或底部时才会注意到。

这里的问题是到达顶部或底部位置后,回收器视图滚动状态仍然保持SCROLL_STATE_SETTLING(1或2秒)。这个状态意味着recyclerview还没有完成一个fling动画。在那种状态下,简单的点击事件只会停止“SETTLING”过程。正常处理以下点击。

看起来有问题...因为“SETTLING”过程应该在到达顶部时立即结束。

RecycleView 类的代码:

 if (mScrollState == SCROLL_STATE_SETTLING) {
                getParent().requestDisallowInterceptTouchEvent(true);
                setScrollState(SCROLL_STATE_DRAGGING);
            }

我已经设法用这段代码修复它

this.addOnScrollListener(object : RecyclerView.OnScrollListener() {

        override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
            val canScrollUp = recyclerView?.canScrollVertically(-1)!!
            val canScrollDown = recyclerView.canScrollVertically(1)
            if (!canScrollUp || !canScrollDown) {
                recyclerView.stopScroll()
            }

        }
    })

我的问题是 1)它是否支持库错误? 2)解决此问题的更正确方法是什么?自定义侦听器对我来说似乎不太好。

PS:我的视图层次结构不是 NestedScrollView 中的 RecyclerView。它是Appbar下的RelativeLayout和带有片段的viewPager。

<RelativeLayout
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v4.view.ViewPager
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>


<FrameLayout  
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#fafafa">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
          />
</FrameLayout>

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    这里是@Revaz 修复的另一种解决方法,因为如果您的子视图没有填满所有可用空间或者您将 PagerSnapHelper 与单个页面一起使用,它将失败。在这种情况下,永远不会调用 onScrolled!

    覆盖您的 LayoutManager 的 startSmoothScroll 例程:

    private final LinearLayoutManager mLayoutManager = new LinearLayoutManager(
            getContext(),
            LinearLayoutManager.HORIZONTAL, false) {
    
        @Override
        public void startSmoothScroll(SmoothScroller smoothScroller) {
            int[] out = mPagerSnapHelper.calculateDistanceToFinalSnap(
                    mLayoutManager, mPagerSnapHelper.findSnapView(mLayoutManager));
            if (out[0] == 0 && out[1] == 0) {
                // workarround "Touch Intercepted after fling”
                // no scroll needed
                MyRecyclerView.this.stopScroll();
                return;
            }
            super.startSmoothScroll(smoothScroller);
        }
    };
    

    .

    setLayoutManager(mLayoutManager);
    

    我认为需要在支持库中修复!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多