Android吸頂tablayout + viewpager

公司要做一個吸頂?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ù) 效果就全出來了。這里就不貼代碼了。

本篇完 更多的優(yōu)化就靠你自己去完善了,畢竟每個人需求不一樣

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容