NestedScrollView+RecyclerView優雅的解決滑動沖突

項目需求,商品詳情頁

詳情.png

通常在做詳情頁的時候,難免需要用到ScrollView嵌套RecyclerView,ScrollView嵌套RecyclerView會存在顯示不全的問題,滑動也不是太流暢,
網上有很多解決滑動沖突的方式,但是筆者今天帶來的是一種比較優雅簡潔的方式,NestedScrollView嵌套RecyclerView,不會存在顯示不全的問題

xml:

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

       <!--其他控件-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="other"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
       
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

上面的代碼只是簡單的布局嵌套而已,但是還有一個小問題,觸摸到RecyclerView的時候滑動還有不流暢,只需

//布局文件的RecyclerView中設置
android:nestedScrollingEnabled="false" 
//或者Java代碼設置
recyclerView.setNestedScrollingEnabled(false);

到此滑動沖突和顯示不全的問題就可以解決了,但是也在此說明一點,這種辦法只是適用于ScrollView中嵌套的那個RecyclerView的內容不是特別多,就像我的項目中,那個評價列表只顯示幾條,這種情況下,用這個辦法不失為一個優雅的解決方式。為啥數據量大的時候就不要使用這種辦法了呢?原因是:NestedScrollView+RecyclerView在顯示上沒什么問題,但會使RecyclerView在初始化就將所有的item都加載出來,換句話說,recyclerVIew的復用機制就不起作用了,所以看項目需求吧

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容