美文网首页
Android吸顶tablayout + viewpager

Android吸顶tablayout + viewpager

作者: jj_huang | 来源:发表于2020-03-20 01:43 被阅读0次

公司要做一个吸顶的效果。之前用过scrollview+tablayout+viewpager的方法把所有的滚动都交给scrollview去做。然后搞2个tablayout 1个放最上面 做吸顶效果用。监听scrollview的滑动。还要切换fragment的时候requestLayout重新计算高度。否则会出现一堆bug。总之很麻烦。所以特地用CoordinatorLayout + 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 显示不全  数值与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盖住  数值与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>

根据上面的布局 之后在activity里把recyclerview 初始化 给点数据 效果就全出来了。这里就不贴代码了。

本篇完 更多的优化就靠你自己去完善了,毕竟每个人需求不一样

相关文章

网友评论

      本文标题:Android吸顶tablayout + viewpager

      本文链接:https://www.haomeiwen.com/subject/vihdyhtx.html