在fragment中用ScrollView嵌套RecyclerView 当只有2个RecyclerView时会很流畅,如果有3个就会很卡,是什么原因? 下面是代码:
<ScrollView
android:id="@+id/scroll_view"
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"
tools:context=".fragments.MainFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="@color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Техника для офиса"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="@+id/recyclerView_popular_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:textColor="@color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Компьютерная техника"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="@+id/recyclerView_competitive_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:textColor="@color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Канцелярские товары"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:visibility="gone"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="@+id/recyclerView_stationery_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
在Fragment中使用ScrollView嵌套RecyclerView时,如果发现只有两个RecyclerView时滚动流畅,但增加到三个RecyclerView就变得卡顿,这可能是由几个原因造成的:
布局重绘问题:ScrollView嵌套多个RecyclerView会增加布局的复杂性,每个RecyclerView都包含大量的View,这可能导致频繁的布局测量和绘制,从而影响性能。特别是当RecyclerView的高度需要在滚动过程中动态计算时(例如,如果使用wrap_content作为高度),这会更加明显。
内存占用:每个RecyclerView都会持有大量数据项的视图缓存,以提高滚动性能。当有三个RecyclerView时,内存占用显著增加,可能导致系统开始进行垃圾回收操作,进而影响UI的流畅度。
滑动冲突:ScrollView与内部的RecyclerView之间可能存在滑动冲突,尤其是在快速滑动或连续滑动多个RecyclerView时,处理不当会导致滑动不顺畅。
资源竞争:多个RecyclerView同时存在可能会导致CPU、GPU资源的竞争,特别是在动画或者复杂的item布局渲染时,这种竞争会更加激烈,从而影响到整体的流畅度。
解决办法:
优化RecyclerView:
setHasFixedSize(true)
:如果你的RecyclerView的大小是固定的,设置这个属性可以避免不必要的measure操作。onBindViewHolder
,减少不必要的绑定操作。DiffUtil
来高效更新数据,减少不必要的重绘。分页加载或懒加载:如果可能,不要一次性加载所有RecyclerView的数据,而是采用分页加载或懒加载的方式,减轻内存压力和初始化负担。
处理滑动冲突:正确处理ScrollView与RecyclerView之间的滑动冲突,确保滑动顺畅。
考虑替代方案:如果三个RecyclerView的内容逻辑上相关,可以考虑合并为一个RecyclerView,并通过不同类型或头部/底部分割器来区分不同的内容区域。
性能优化工具:使用Android Studio的Profiler工具分析具体哪里消耗了性能,比如CPU、GPU、内存等,根据分析结果做针对性优化。
限制视图数量:考虑是否真的需要三个独立的RecyclerView,有时候通过优化布局设计,使用更轻量级的组件或改进数据展示方式,可以有效减少视图数量,提升性能。