如何实现具有两个固定视图的“双”折叠式工具栏

7
我试图实现图片中展示的布局。
这是我想要的效果
因此,我想要有两个视图,在用户滚动到一定位置后固定在屏幕顶部。其他视图应该折叠起来,直到完全隐藏(除了View 3,它基本上是布局的主要视图)。到目前为止,我尝试了几种方法。
其中一个方法有些可行,使用可折叠工具栏,将可固定视图1和可固定视图2固定到其他视图上(我附上了代码片段,让您知道我已经实现了什么)。这种方法的问题在于,因为这些可固定视图不在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">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <View
                android:id="@+id/view_1"
                android:layout_width="match_parent"
                android:layout_marginBottom="32dp"
                android:layout_height="200dp"
                android:background="#0071BD"
                app:layout_collapseMode="parallax">
            </View>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:layout_marginTop="32dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".ScrollingActivity">

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

            <View
                android:id="@+id/view_2"
                android:layout_width="wrap_content"
                android:background="#5495BD"
                android:layout_height="300dp"/>

            <View
                android:id="@+id/pinnable_view_2_anchor_view"
                android:layout_width="match_parent"
                android:layout_height="41dp"
                android:background="@null" />

            <View
                android:id="@+id/view_3"
                android:layout_width="match_parent"
                android:background="#57B1BC"
                android:layout_height="1000dp" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

    <View
        android:id="@+id/pinnable_view_1"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="#00015E"
        android:nestedScrollingEnabled="true"
        app:layout_anchor="@id/app_bar_layout_view"
        app:layout_anchorGravity="bottom">
    </View>

    <View
        android:id="@+id/pinnable_view_2"
        android:layout_width="match_parent"
        android:background="#000002"
        android:layout_height="40dp"
        android:layout_marginTop="64dp"
        app:layout_anchor="@id/pinnable_view_2_anchor_view"
        app:layout_anchorGravity="center">
    </View>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

你好,你是否正在尝试实现一个可以固定标题的事项列表? - ravi
不是完全一样的。这些视图内部的内容完全不同,但请详细说明,因为这可能也有帮助 ;) - baltekg
如果这是一个列表并且需要固定标题,我可以帮忙处理。祝你好运。 - ravi
1个回答

2
也许现在已经有点晚了,但是你可以从这里检查代码https://github.com/Y-E-P/DoubleCollapsingToolbar,或许它会帮助到其他遇到同样问题的开发者。我准备了两个使用示例,你可以在你的项目中自由使用这段代码。以下是结果。

enter image description here

享受吧!


1
在 lifo_ordering 分支上有一个问题:在 fling 手势之后,第一个工具栏不会展开 :( - acbelter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接