【发布时间】: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