1. 程式人生 > 其它 >《Android》CoordinatorLayout+Appbarlayout實現滑動隱藏頂部

《Android》CoordinatorLayout+Appbarlayout實現滑動隱藏頂部

首先:xml佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation
="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width
="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width
="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_scrollFlags="scroll|enterAlways"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是一個按鈕" /> </LinearLayout> </com.google.android.material.appbar.AppBarLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/audio_list_rv" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </FrameLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout> </LinearLayout> </LinearLayout>

一.外面用一層Coordinatorlayout包裹,然後Appbarlayout包裹滑動要隱藏的View或者Viewgroup

注意:Appbarlayout裡的View擁有屬性app:layout_scrollFlags="scroll|enterAlways",意思就是這個頂部View在recyclerView滑動的時候隱藏,而且擁有這個屬性的頂部view一定要在Appbarlayout裡的最上面,而且下面也不能放其他view或者viewgroup,這裡app:layout_scrollFlags="scroll|enterAlways"是放在Appbarlayout裡的Linearyout,Linearyout上下都沒有其他view或者viewgroup。

二.app:layout_behavior="@string/appbar_scrolling_view_behavior" 這個屬性放在你要滑動的佈局裡,就是佈局裡要有能滑動屬性的view,這裡使用了recyclerView,

注意:app:layout_behavior="@string/appbar_scrolling_view_behavior" 這個屬性會把view放在Appbarlayout底部,這裡recyclerView擁有這個屬性,所有recyclerView會在Appbarlayout底部,

而fragment佈局有一部分會在Appbarlayout覆蓋住,下面是放在Fragment和recyclerView的測試圖:

放在recyclerView裡:

放在Fragment裡:

app:layout_scrollFlags的值有下面五種可選:

  • scroll

注意事項

  • 這個標誌位是其它四個標誌位生效的前提
  • 帶有scroll標誌位的子View必須放在AppBarLayout中的最前面
  • exitUntilCollapsed
  • enterAlways
  • enterAlwaysCollapsed
  • snap

這五個標誌位在子view中的組合及效果有:

  1. app:layout_scrollFlags="scroll"
    效果:

設定了scroll的子View可以在滾動後收起,而沒有設定的則不可以。
在手指向上移動的時候,優先收起AppBarLayout中的可收起View,當它處於收起狀態時,下面的列表內容才開始向尾部滾動。
在手指往下移動的時候,優先讓下面的列表內容向頂部滾動,當列表滾動到頂端時,AppBarLayout的可收起View才展開。

  1. app:layout_scrollFlags="scroll|exitUntilCollapsed"
    另外設定一下android:minHeight="50dp"
    效果:

手指向上移動的時候,會優先收起AppBarLayout中的子View,而收起的最小高度為0,如果想收起時仍然保留部分可見,那麼就需要使用exitUntilCollapsed + minHeight屬性,minHeight就決定了收起時的最小高度是多少

  1. app:layout_scrollFlags="scroll|enterAlways"
    效果:

enterAlways則決定了手指向下移動時的行為。預設情況下,在手指向下移動時,會優先讓列表滾動到頂部,而如果設定了enterAlways,那麼會優先讓AppBarLayout中的子View滾動到展開。

  1. app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
    效果:

優先滾動AppBarLayout中的子View,但是並不是滾動到全部展開,而是隻滾動到minHeight
AppBarLayout中的子View滾動到minHeight之後,開始滾動列表,直到列表滾動到頭部
列表滾動到頭部之後,開始滾動AppBarLayout中的子View,直到它完全展開

  1. app:layout_scrollFlags="scroll|snap"
    效果:

預設情況下,在手指從螢幕上抬起之後,AppBarLayout中子View的狀態就不會變化了,而如果我們設定了snap標誌位,那麼在手指抬起之後,會根據子View當前的偏移量,決定是讓它變為收起還是展開狀態