ScrollView 嵌套RecyclerView

本文介绍了在Android开发中如何将RecyclerView嵌套在ScrollView内,分别通过使用NestScrollView和ScrollView两种方式。在使用NestScrollView时遇到了界面滑动卡顿且失去惯性滑动的问题,解决方法是调整RecyclerView的设置。而使用ScrollView则是在RelativeLayout中放置RecyclerView,目前在Android 4.4的Pad上运行良好,但对未来可能出现的问题持保留态度。

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

目前使用有两种方法:

1、使用新控件NestScrollView 嵌套RecyclerView,使用方法与一般ScrillView嵌套条目一样

xml:

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="30px"
        android:layout_marginRight="30px"
        android:layout_marginTop="25px"
        android:background="@color/white">

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

				<TextView
                    android:id="@+id/title8"
                    android:visibility="invisible"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="title8"
                    android:textSize="@dimen/sp16"/>

              
                <TextView
                    android:id="@+id/title2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="title2"
                    android:textSize="@dimen/sp16"/>


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/testing_activity_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20px"
                    android:layout_marginRight="20px"
                    android:visibility="invisible"/>
          
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

在代码中的调用和一般的RecyclerView的使用是一样的,没有任何改变。但是我用的时候界面滑动出现了卡顿现象,而且没有惯性滑动了。

解决方案:代码中RecyclerView 设置

LinearLayoutManager layoutManager = new LinearLayoutManager(TestingActivity.this);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);

        test_recycler.setLayoutManager(layoutManager);
        test_recycler.setHasFixedSize(true);
        test_recycler.setNestedScrollingEnabled(false);

2、使用ScrollView

使用ScrollViedw嵌套RelativeLayout,RelativeLayout里面放RecyclerView。

xml:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="30px"
        android:layout_marginRight="30px"
        android:layout_marginTop="25px"
        android:background="@color/white">

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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="blocksDescendants">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/testing_activity_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20px"
                    android:layout_marginRight="20px"
                    android:visibility="visible"/>

            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

在代码中也是没做处理,感觉可能和我的Pad的Android有关系,使用的Android4.4的pad,不清楚后续会不会出现问题,暂时到这,后续更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值