NestedScrollView + RecyclerView 嵌套产生卡顿处理

博客指出不建议在数据量大时使用NestedScrollView嵌套RecyclerView,因会使RecyclerView的item展开、复用失效。还提到使用这种嵌套会出现卡顿情况,记录了针对老版本手机(api21-)的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

特别说明:
对于NestedScrollView嵌套RecylcerView不建议在数据量很大的情况下使用,因为这种嵌套会导致RecyclerView的item被展开,导致复用失效。

在使用NestedScrollView + RecyclerView 嵌套的时候,会出现卡顿情况,大致布局如下,在网上找了处理方式,在此记录下

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/tvSpace">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include layout="@layout/forward_banner_layout" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvContentList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="false" />   //在api21之后生效,可以确保api21+的不会卡顿
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

对于老版本手机(api21-),处理方式如下

val layoutManager = LinearLayoutManager(this)
 layoutManager.isSmoothScrollbarEnabled = true
 layoutManager.isAutoMeasureEnabled = true
 rvContentList.layoutManager = layoutManager
 rvContentList.setHasFixedSize(true)
 rvContentList.isNestedScrollingEnabled = false
 rvContentList.adapter = mAdapter
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值