android 基于 AppBarLayout 实现吸顶交互

645 阅读1分钟

布局方式可以参考一下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
    <!-- 标题栏部分 -->
    <com.lujianfei.module_plugin_base.widget.PluginToolBar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:layout_constraintTop_toTopOf="parent" />
    <!-- 组件协调联动布局 -->
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!-- 可折叠区域 -->
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collsping_Toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:background="#ff0000"
                    android:text="可折叠区域"
                    android:textColor="@color/white"
                    android:textStyle="bold"
                    android:textSize="25sp"
                    android:gravity="center"
                    />
            </com.google.android.material.appbar.CollapsingToolbarLayout>

            <!-- 不可折叠区域 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="pin">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="56dp"
                    android:background="#dddddd"
                    android:textColor="@color/black"
                    android:gravity="center_vertical"
                    android:padding="15dp"
                    android:text="不可折叠区域 (用作吸顶标题)"/>
            </LinearLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    <!-- 组件协调联动布局 end -->
</androidx.constraintlayout.widget.ConstraintLayout>

预览效果

applayout 00_00_00-00_00_30.gif

demo展示apk下载链接