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