当我们布局遇到一些复杂的布局时,或是一些特殊要求的布局时,会用到RecyclerView嵌套RecyclerView或者ScrollView嵌套RecyclerView的情况,但是使用这两种嵌套方式进场会遇到滑动冲突,焦点抢占或是RecyclerView内容显示不全等问题,那么怎么解决呢?
1.RecyclerView嵌套RecyclerView
我们经常在项目的类似朋友圈场景中使用RecyclerView嵌套RecyclerView,但是焦点抢占问题是这种嵌套的一个大问题。
上图中第一张图是嵌套时焦点抢占原因默认打开页面的显示情况,第二张图是我们想要的默认打开页面的显示情况。
先看一下布局文件
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.joh.demo.scrowviewdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.joh.demo.scrowviewdemo.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/text_one" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/text_one" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
出现上图中的情况是因为,item布局中的RecyclerView抢占了外层RecyclerView的焦点,解决方法也很简单,只需要把焦点还给外层RecyclerView就可以了,那么如何做呢?只需要在item_layout这个布局文件的最外层布局中添加以下代码即可
android:focusable="true"
android:focusableInTouchMode="true"
然后编辑运行,你会发现问题已经解决了
如果使用RecyclerView添加头布局时,记得在头布局文件的最外层布局添加上面两行代码,不然会出现item抢占头布局焦点的问题
2.ScrollView嵌套RecyclerView
ScrollView嵌套RecyclerView常见的问题是滑动冲突和RecyclerView内容显示不全的问题,笔者遇到这个问题时在网上找了好多,大多建议重写LayoutManager(相当于ScrollView嵌套ListView时,直接测量ListView的高度,禁止ListView的滑动事件一样),这种方法也的确可行,但是需要重写LayoutManager,太麻烦。那么有没有可替代的方案呢?NestedScrollView解决了ScrollView嵌套RecyclerView的滑动冲突问题,如果需要使用ScrollView嵌套RecyclerView的布局时,建议大家可以考虑试着用NestedScrollView替代一下ScrollView,这样就可以解决掉滑动冲突和RecyclerView内容显示不全的问题。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.joh.demo.scrowviewdemo.MainActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
使用NestedScrollView嵌套RecyclerView,需要在RecyclerView的属性中添加android:nestedScrollingEnabled=“false”,不然还是会出现滑动冲突。