首页 文章

更好的方法来修复“触摸后触摸截获”

提问于
浏览
0

我有同样的问题here . 问题是,在发生投掷后,RecyclerView儿童不会获得触摸事件 . 只有当Recyclerview达到顶部或底部时才会显而易见 .

这里的问题是,在到达顶部或底部位置后,回收者视图滚动状态仍然是 SCROLL_STATE_SETTLING (持续1或2秒) . 此状态表示 recyclerview 尚未完成动画动画 . 在该状态下,简单点击事件仅停止"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>

1 回答

  • 0

    这里修复了@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);
    

    在我看来,需要在支持库中修复!

相关问题