开发者社区 问答 正文

在fragment中用ScrollView嵌套RecyclerView

在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>

展开
收起
1403297683414040 2019-11-15 14:14:47 685 分享 版权
阿里云 AI 助理回答

在Fragment中使用ScrollView嵌套RecyclerView时,如果发现只有两个RecyclerView时滚动流畅,但增加到三个RecyclerView就变得卡顿,这可能是由几个原因造成的:

  1. 布局重绘问题:ScrollView嵌套多个RecyclerView会增加布局的复杂性,每个RecyclerView都包含大量的View,这可能导致频繁的布局测量和绘制,从而影响性能。特别是当RecyclerView的高度需要在滚动过程中动态计算时(例如,如果使用wrap_content作为高度),这会更加明显。

  2. 内存占用:每个RecyclerView都会持有大量数据项的视图缓存,以提高滚动性能。当有三个RecyclerView时,内存占用显著增加,可能导致系统开始进行垃圾回收操作,进而影响UI的流畅度。

  3. 滑动冲突:ScrollView与内部的RecyclerView之间可能存在滑动冲突,尤其是在快速滑动或连续滑动多个RecyclerView时,处理不当会导致滑动不顺畅。

  4. 资源竞争:多个RecyclerView同时存在可能会导致CPU、GPU资源的竞争,特别是在动画或者复杂的item布局渲染时,这种竞争会更加激烈,从而影响到整体的流畅度。

解决办法:

  1. 优化RecyclerView

    • 使用setHasFixedSize(true):如果你的RecyclerView的大小是固定的,设置这个属性可以避免不必要的measure操作。
    • 优化Adapter中的onBindViewHolder,减少不必要的绑定操作。
    • 使用DiffUtil来高效更新数据,减少不必要的重绘。
  2. 分页加载或懒加载:如果可能,不要一次性加载所有RecyclerView的数据,而是采用分页加载或懒加载的方式,减轻内存压力和初始化负担。

  3. 处理滑动冲突:正确处理ScrollView与RecyclerView之间的滑动冲突,确保滑动顺畅。

  4. 考虑替代方案:如果三个RecyclerView的内容逻辑上相关,可以考虑合并为一个RecyclerView,并通过不同类型或头部/底部分割器来区分不同的内容区域。

  5. 性能优化工具:使用Android Studio的Profiler工具分析具体哪里消耗了性能,比如CPU、GPU、内存等,根据分析结果做针对性优化。

  6. 限制视图数量:考虑是否真的需要三个独立的RecyclerView,有时候通过优化布局设计,使用更轻量级的组件或改进数据展示方式,可以有效减少视图数量,提升性能。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
问答地址: