项目需求,商品详情页
通常在做详情页的时候,难免需要用到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的复用机制就不起作用了,所以看项目需求吧