如何实现Android滚动时指定UI折叠,CollapsingToolbarLayout配置不生效
方案说明
你的布局无法触发折叠的核心问题有4处,修改后即可实现仅折叠指定区域的效果:
- 父容器优先级问题:所有内容被包裹在一个设置了
app:layout_collapseMode="parallax"的大LinearLayout内,CollapsingToolbarLayout只会识别直接子View的collapseMode属性,内部子View单独设置的pin/parallax属性不会生效。 - 高度配置错误:CollapsingToolbarLayout的
android:layout_height设置为match_parent会导致滚动范围计算异常,需要改为wrap_content或者明确设置展开总高度。 - 缺少联动滚动视图:AppBarLayout的折叠逻辑依赖带
app:layout_behavior="@string/appbar_scrolling_view_behavior"的可滚动控件(NestedScrollView、RecyclerView等),你当前把滚动布局代码注释了,自然无法触发滚动折叠效果。 - 固定区域层级错误:需要固定的toolbar、充值按钮、底部分割线不能放在可折叠容器内,需要直接作为CollapsingToolbarLayout的直接子View,设置
app:layout_collapseMode="pin"。
核心修改后的布局示例
<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:fitsSystemWindows="true" android:background="@color/white_ffffff" tools:context=".MainActivity"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/Theme.MyApplication.AppBarOverlay" > <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" android:background="@color/white_ffffff" app:toolbarId="@+id/toolbar" > <!-- 需要折叠的区域:用户信息+资产卡片 单独作为直接子View设置parallax --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_collapseMode="parallax" android:paddingTop="?attr/actionBarSize"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingStart="@dimen/dp_33" android:paddingTop="@dimen/dp_16" android:paddingEnd="@dimen/dp_33" android:paddingBottom="@dimen/dp_16" > <!-- 原有用户信息布局内容保持不变 --> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/imgAvatar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_default_avatar" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginStart="@dimen/dp_20" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/imgAvatar" app:layout_constraintTop_toTopOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="김근육" android:textColor="@color/black_000000" android:textSize="@dimen/text_18sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="abc@gmail.com" android:textSize="@dimen/text_12sp" /> </LinearLayout> <ImageView android:id="@+id/imgSeeMore" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="TODO" android:src="@drawable/ic_arrow_right" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/dp_16" android:layout_marginEnd="@dimen/dp_16" android:background="@drawable/ic_bg_colapsing_section"> <!-- 原有资产卡片布局内容保持不变,Guideline、分割线、功能块代码和原版本一致即可 --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.1675" /> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout> <!-- 固定区域1:toolbar 直接作为子View设置pin --> <include android:id="@+id/toolbar" layout="@layout/layout_toolbar_2" app:layout_collapseMode="pin" /> <!-- 固定区域2:充值按钮 直接作为子View设置pin,调整marginTop匹配布局位置 --> <Button android:id="@+id/btnCashCharge" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/cash_charge" android:textSize="@dimen/text_18sp" android:layout_marginStart="@dimen/dp_16" android:layout_marginEnd="@dimen/dp_16" android:background="@drawable/bg_border_nomal" android:backgroundTint="@color/blue_1953E9" android:textColor="@color/white_ffffff" android:layout_marginTop="220dp" app:layout_collapseMode="pin" /> <!-- 固定区域3:底部分割线 直接作为子View设置pin --> <View android:layout_width="match_parent" android:layout_height="@dimen/dp_8" android:paddingBottom="@dimen/dp_16" android:background="@color/gray_F5F5F5" android:layout_marginTop="260dp" app:layout_collapseMode="pin" /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <!-- 取消注释滚动布局,添加behavior属性联动折叠 --> <include layout="@layout/layout_my_page_scrolling" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_marginEnd="@dimen/dp_16" android:layout_marginBottom="@dimen/dp_16" android:backgroundTint="@color/blue_1953E9" android:src="@drawable/ic_camera" android:contentDescription="TODO" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
调整时注意修改固定控件的layout_marginTop数值,匹配你实际的可折叠区域高度即可。
内容的提问来源于stack exchange,提问作者neo
相关产品推荐
相关产品推荐

