更好的方法来解决“触摸后一触即发”
问题描述:
我有同样的问题,在这个问题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:我的视图层次结构不是RecyclerView里面的NestedScrollView。它是AppBar和ViewPager下的RelativeLayout片段。
<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>
答
这里另一个解决方法@Revaz修复,因为如果你的孩子意见不填满所有可用的空间,或使用PagerSnapHelper与单页就会失败。在这种情况下,滚动将永远不会被调用!
覆盖您的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);
需要在支持库中修复我的意见!