ScrollView 嵌套 RecyclerView 冲突问题

开发中使用ScrollView 嵌套 RecyclerView 时遇到了滑动冲突和显示不全问题。自己尝试解决了一下。

由于项目需求,需要 ScrollView 高度是 wrap_content,ScrollView 内是一个 LinearLayout ,LinearLayout 里添加了多个 RecyclerView 。SrcollView 有一个最大高度 maxHeight 。

对于可能出现的 滑动冲突和卡顿,我使用了下面的方法解决;

final RecyclerView rvFilterItems = (RecyclerView) mFilterView.findViewById(R.id.rv_filter_items);
GridLayoutManager layoutManager = new GridLayoutManager(mP.mContext, mP.mSpanCount, OrientationHelper.VERTICAL, false){
    
    
    @Override
    public boolean canScrollVertically() {
    
    
        return false;
    }
};

rvFilterItems.setLayoutManager(layoutManager);
rvFilterItems.setNestedScrollingEnabled(false);
rvFilterItems.setHasFixedSize(true);

发现 ScrollView 内容的高度在 maxHeight 附近时, RecyclerView 的 items 显示不全。内容明显大于或小于 maxHeight 时,貌似没有问题。

为了解决 显示不全的问题,看了几篇博客,初步解决了问题。

加了两行代码:layoutManager.setSmoothScrollbarEnabled(true); layoutManager.setAutoMeasureEnabled(true);

final RecyclerView rvFilterItems = (RecyclerView) mFilterView.findViewById(R.id.rv_filter_items);
GridLayoutManager layoutManager = new GridLayoutManager(mP.mContext, mP.mSpanCount, OrientationHelper.VERTICAL, false){
    
    
    @Override
    public boolean canScrollVertically() {
    
    
        return false;
    }
};
layoutManager.setSmoothScrollbarEnabled(true);
layoutManager.setAutoMeasureEnabled(true);
rvFilterItems.setLayoutManager(layoutManager);
rvFilterItems.setNestedScrollingEnabled(false);
rvFilterItems.setHasFixedSize(true);

布局文件中 ScrollView 换成了 android.support.v4.widget.NestedScrollView,并加上了android:fillViewport="true"

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:fillViewport="true"
    android:scrollbars="none">
    <LinearLayout
        android:id="@+id/ll_sel_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

最终基本解决了问题。但是,触摸滑动有时候会失去惯性滑动,即手指离开屏幕,马上就不滑动了,有不流畅的感觉。
参考博客 NestedScrollView多层嵌套滑动冲突解决,让 NestedScrollView 拦截事件,尝试解决滑动问题,没有明显的改善。

参考:

  1. NestedScrollView 的问题记录
  2. 解决ScrollView嵌套RecyclerView的显示及滑动问题

猜你喜欢

转载自blog.csdn.net/wangxiaocheng16/article/details/100161084