公司要做一個吸頂?shù)男Ч?。之前用過scrollview+tablayout+viewpager的方法把所有的滾動都交給scrollview去做。然后搞2個tablayout 1個放最上面 做吸頂效果用。監(jiān)聽scrollview的滑動。還要切換fragment的時候requestLayout重新計算高度。否則會出現(xiàn)一堆bug??傊苈闊K蕴氐赜肅oordinatorLayout + tablayout+viewpager來寫 為了能快速看出效果這里我用的是RecyclerView 替代Viewpager ,TextView替代Tablayout 先上布局
記錄這篇文章的目的是為了爬坑,希望對你有幫助吧 多看注解
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<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:background="#ffffff"
>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="45dp" //不寫上這個的話 recyclerview 顯示不全 數(shù)值與tablayout的高度一致
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="top"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5"
android:layout_marginBottom="45dp" //這個是為了把布局增大 否則這個布局里的底部會被tablayout蓋住 數(shù)值與tablayout的高度一致
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="我是banner"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:background="#B00020"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="我是隨便寫的"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:background="#018786"
/>
</LinearLayout>
<TextView
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="bottom"
android:background="#ff0000"
android:text="tablayout"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20dp"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- app:layout_behavior="@string/appbar_scrolling_view_behavior" 這個必加不要問為什么 問了就是不知道 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
根據(jù)上面的布局 之后在activity里把recyclerview 初始化 給點數(shù)據(jù) 效果就全出來了。這里就不貼代碼了。