You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CoordinatorLayout中NestedScrollView与ViewPager无法正常滚动求助

问题描述

使用CoordinatorLayout布局,包含ViewPager、RecyclerView、TabLayout及其他组件,期望页面能正常滚动,但实际无法实现(问题演示如下):

问题演示动图

已尝试多种调整方案均未奏效,最后尝试将包含横向RecyclerView的特惠模块放入NestedScrollView中,问题仍未解决。原布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
    android:background="@color/white"
    tools:context=".home.ui.views.HomeFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/white"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|snap"
            app:toolbarId="@id/toolbar">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/white"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/textView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="4dp"
                        android:fontFamily="@font/worksans_regular"
                        android:text="Discover"
                        android:textColor="@color/purple_S600"
                        android:textSize="24sp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <Button
                        android:id="@+id/button"
                        android:layout_width="26dp"
                        android:layout_height="32dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginEnd="16dp"
                        android:backgroundTint="@color/white_grey_s400"
                        android:drawableLeft="@drawable/account_icon"
                        android:padding="1dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>

            </com.google.android.material.appbar.MaterialToolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/search_textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="38dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="4dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/search_edittext_shape"
            android:textColorHint="@color/purple_grey_S600"
            app:boxStrokeWidth="0dp"
            app:boxStrokeWidthFocused="0dp"
            app:endIconDrawable="@drawable/close_icon"
            app:endIconMode="clear_text"
            app:endIconTint="@color/purple_S600"
            app:hintEnabled="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:startIconDrawable="@drawable/search_icon"
            app:startIconTint="@color/purple_S600">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/home_search_tf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Search"
                android:inputType="text"
                android:padding="0dp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/categories_tabs"
            style="@style/CategoriesTabLayourStyle"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:visibility="visible" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/home_constraint"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nested_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_gravity="start"
                    android:background="@color/yellow_S600"
                    android:fontFamily="@font/worksans_semibold"
                    android:text="Deals      "
                    android:textColor="@color/purple_S600"
                    android:textSize="20sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="-28dp"
                    android:src="@drawable/deals_arrow"
                    app:layout_constraintBottom_toBottomOf="@+id/textView2"
                    app:layout_constraintStart_toEndOf="@+id/textView2"
                    app:layout_constraintTop_toTopOf="@+id/textView2" />

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:fontFamily="@font/inter_regular"
                    android:text="UP to 75% off"
                    android:textColor="@color/purple_S600"
                    android:textSize="14sp"
                    app:layout_constraintBottom_toBottomOf="@+id/textView2"
                    app:layout_constraintStart_toEndOf="@+id/textView2"
                    app:layout_constraintTop_toTopOf="@+id/textView2" />

                <ImageView
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="2dp"
                    android:src="@drawable/flame_icon"
                    app:layout_constraintBottom_toBottomOf="@+id/textView5"
                    app:layout_constraintStart_toEndOf="@+id/textView5"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/deals_recyclerview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:layout_marginStart="8dp"
                    android:background="@color/white"
                    android:clipToPadding="false"
                    android:orientation="horizontal"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textView2"
                    tools:listitem="@layout/deal_product" />
            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/nested_view" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
解决方案

核心问题分析

  1. 滚动容器高度错误:home_constraint作为CoordinatorLayout的滚动子View,设置wrap_content无法填充剩余屏幕空间,导致滚动事件无法正常触发。
  2. 多余嵌套容器:横向RecyclerView不需要放在NestedScrollView中,该容器会拦截纵向滚动事件,引发冲突。
  3. Behavior属性误用:TextInputLayout属于AppBarLayout子View,不应设置appbar_scrolling_view_behavior,会打乱布局计算逻辑。
  4. ViewPager高度未填充:wrap_content无法让ViewPager占据剩余空间,内部滚动内容无法正常触发整体滚动。

修改后的布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
    android:background="@color/white"
    tools:context=".home.ui.views.HomeFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/white"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|snap"
            app:toolbarId="@id/toolbar">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/white"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/textView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="4dp"
                        android:fontFamily="@font/worksans_regular"
                        android:text="Discover"
                        android:textColor="@color/purple_S600"
                        android:textSize="24sp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <Button
                        android:id="@+id/button"
                        android:layout_width="26dp"
                        android:layout_height="32dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginEnd="16dp"
                        android:backgroundTint="@color/white_grey_s400"
                        android:drawableLeft="@drawable/account_icon"
                        android:padding="1dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>

            </com.google.android.material.appbar.MaterialToolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/search_textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="38dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="4dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/search_edittext_shape"
            android:textColorHint="@color/purple_grey_S600"
            app:boxStrokeWidth="0dp"
            app:boxStrokeWidthFocused="0dp"
            app:endIconDrawable="@drawable/close_icon"
            app:endIconMode="clear_text"
            app:endIconTint="@color/purple_S600"
            app:hintEnabled="false"
            app:startIconDrawable="@drawable/search_icon"
            app:startIconTint="@color/purple_S600">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/home_search_tf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Search"
                android:inputType="text"
                android:padding="0dp" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/categories_tabs"
            style="@style/CategoriesTabLayourStyle"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:visibility="visible" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/home_constraint"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/deals_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:background="@color/yellow_S600"
                android:fontFamily="@font/worksans_semibold"
                android:text="Deals      "
                android:textColor="@color/purple_S600"
                android:textSize="20sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="-28dp"
                android:src="@drawable/deals_arrow"
                app:layout_constraintBottom_toBottomOf="@+id/textView2"
                app:layout_constraintStart_toEndOf="@+id/textView2"
                app:layout_constraintTop_toTopOf="@+id/textView2" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:fontFamily="@font/inter_regular"
                android:text="UP to 75% off"
                android:textColor="@color/purple_S600"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="@+id/textView2"
                app:layout_constraintStart_toEndOf="@+id/textView2"
                app:layout_constraintTop_toTopOf="@+id/textView2" />

            <ImageView
                android:layout_width="16dp
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:16:28