項目需求,商品詳情頁
詳情.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的復用機制就不起作用了,所以看項目需求吧