安卓开发中嵌套滚动视图的使用

有如下文件:

(1)forecast.xml(存放未来几天天气的布局页面)

(2)activity_weather.xml(存放今日天气、未来几天天气、生活指数等布局)

        现在要实现在activity_weather.xml文件中对未来几天天气布局界面的嵌套滑动,我选择使用 Android 中的一个视图组件 —— NestedScrollView,它继承自ScrollView并添加了嵌套滚动的支持。

        嵌套滚动允许一个滚动的视图(如 NestedScrollView RecyclerView)在另一个滚动的父视图中协同工作,而不会产生滚动冲突。这在需要嵌套滚动行为(如滚动头部或滚动条共享)的复杂布局中非常有用。

文件activity_weather.xml

DrawerLayout 布局中设置有ScrollView控件,ScrollView控件中包含:

@+id/now(今日天气) 

@+id/forecast(未来几天天气) 

 @+id/life_index(今日生活指数)

这三个自定义布局,现要实现对于@+id/forecast布局的嵌套滚动

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.forecastweather.ui.weather.WeatherActivity">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:id="@+id/weatherLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:scrollbars="none"
            android:visibility="invisible">

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

                <include
                    android:id="@+id/now"
                    layout="@layout/now" />

                <include
                    android:id="@+id/forecast"
                    layout="@layout/forecast" />

                <include
                    android:id="@+id/life_index"
                    layout="@layout/life_index" />
                <!--直接导入对应的布局-->
            </LinearLayout>

        </ScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/design_default_color_primary"
        android:clickable="true"
        android:focusable="true">

        <fragment
            android:id="@+id/placeFragment"
            android:name="com.example.forecastweather.ui.place.PlaceFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="25dp" />

    </FrameLayout>

</androidx.drawerlayout.widget.DrawerLayout>



文件forecast.xml

LinearLayout外部嵌套一个NestedScrollView,设置方式和ScrollView的方法基本相同

<?xml version="1.0" encoding="utf-8"?><!--forecast:预测-->
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:layout_marginRight="15dp"
    app:cardCornerRadius="4dp">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20sp"
            android:layout_marginBottom="20sp"
            android:text="@string/forecast"
            android:textColor="?android:attr/textColorPrimary"
            android:textSize="20sp" />

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:overScrollMode="never"
            android:scrollbars="none"
            android:visibility="visible">

            <LinearLayout
                android:id="@+id/forecastLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" />

        </androidx.core.widget.NestedScrollView>

    </LinearLayout>

</com.google.android.material.card.MaterialCardView>

注意:在此处需要设置NestedScrollView需要注意下面几个问题否则可能会出现设置完控件不能实现嵌套滚动的效果:

  1. LinearLayout高度问题:如果 LinearLayout 的高度被设置为 match_parent,并且它的父容器(即 NestedScrollView)也是 match_parent,那么 LinearLayout 的高度将等于 NestedScrollView 的高度,这通常不会触发滚动,因为内容没有超出可视区域。确保 LinearLayout 的高度是由其内容决定的,而不是直接设置为 match_parent。                  
  2. 内容填充问题:如果 LinearLayout 的子视图没有正确填充或者它们的布局参数(如 layout_height)没有设置为 wrap_content 或具体的值,那么它们可能不会被正确地计入 LinearLayout 的总高度中。                                                                                              
  3. NestedScrollView高度限制:确保 NestedScrollView 的父容器(可能是 CoordinatorLayout、RelativeLayout、LinearLayout 等)没有限制 NestedScrollView 的高度。                                                                                                                                  
  4. NestedScrollView嵌套问题:虽然 NestedScrollView 支持嵌套滚动,但如果将它嵌套在另一个滚动视图中(如 ScrollView),没有正确配置,也有可能会导致滚动问题。             
  5. 权重问题:如果 LinearLayout 的子视图使用了权重(layout_weight),并且没有正确设置高度,可能会导致滚动问题。

设置正确后,运行代码就可以完成对于forecast.xml文件中的LinearLayout的嵌套滚动

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值