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的顯示及滑動問題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章