RecyclerView嵌套问题
来源:互联网 发布:unity3d输出日志文件 编辑:程序博客网 时间:2024/06/16 05:07
当我们布局遇到一些复杂的布局时,或是一些特殊要求的布局时,会用到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”,不然还是会出现滑动冲突。
笔者的demo已经上传,有需要的可以下载
- ScrollView嵌套recyclerView问题
- scrollview嵌套recyclerview 问题
- ScrollView嵌套RecyclerView 问题
- RecyclerView嵌套问题ScrollView
- ScrollView嵌套recyclerView问题
- ScrollView嵌套recyclerView问题
- RecyclerView嵌套问题
- ScrollView嵌套RecyclerView,RecyclerView不显示问题
- 解决RecyclerView嵌套RecyclerView位移问题
- ScrollView嵌套ListView,RecyclerView问题
- 修复RecyclerView嵌套滚动问题
- ScrollView中嵌套RecyclerView问题
- Scrollview中嵌套Recyclerview问题
- ScrollView和RecyclerView嵌套问题
- ScrollView 和 RecyclerView 嵌套问题
- ViewPager嵌套RecyclerView刷新问题
- RecyclerView嵌套Spinner onItemSelected问题
- ScrolView和RecyclerView嵌套使用的问题
- 搭建SSH框架
- base64编码解码js
- 线性回归模型采用梯度下降算法求最优解
- Tensorflow安装及更新
- 我的第一篇博客
- RecyclerView嵌套问题
- 搭建vue.js开发环境过程
- win7与虚拟机中linux共享文件方法
- 无限轮播自动获取图片
- Hashset和treeset
- AutoFac 的使用
- charles之配置
- HDU 5168 Legal path 最短路 OR DP
- Git 常用整理