Android-CoordinatorLayout.……

CoordinatorLayout配合AppBarLayout、

2020-10-27  本文已影响0人  hao_developer

效果如图:


c8f2bb64469bae61739745241ba7b8a1.gif

首先介绍上述几个控件在使用的时候的注意点:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

他们的层次关系如下图(图是网上扣来的)

image.png

以下是我自己写的布局

<?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/tabLay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            app:contentScrim="#808080"
            app:statusBarScrim  ="#808080"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:collapsedTitleGravity="center"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:title="123"
            app:titleEnabled="false">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <View
                    android:id="@+id/oneView"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:background="@color/colorPrimary" />

                <View
                    android:id="@+id/twoView"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:background="@color/colorPrimaryDark" />

                <View
                    android:id="@+id/threeView"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:background="@android:color/holo_green_dark" />

            </LinearLayout>

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginTop="40dp"
                app:layout_collapseMode="pin"
                app:title="coordinator" />


        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabContent"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@android:color/holo_purple">

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="惊喜吗" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="惊喜吗" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="惊喜吗" />

        </com.google.android.material.tabs.TabLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

如果想需要实现沉浸式:

image.png
<!-- 设置theme -->
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
       <item name="android:windowTranslucentStatus" tools:ignore="NewApi">true</item>
       <item name="android:windowTranslucentNavigation" tools:ignore="NewApi">true</item>
</style>

给相对应的activity设置主题

image.png
android:theme="@style/TranslucentTheme"
tabLay.addOnOffsetChangedListener(this)
override fun onOffsetChanged(bar: AppBarLayout?, height: Int) {
        val heghtScroll = abs(height)
        val oneHeight = oneView.height
        val twoHeight = twoView.height
        val threeHeight = threeView.height
        val fourHeight = tabContent.height
        if (heghtScroll == 0){
            Log.e("tag","$height******头部")
            initView()
        }else if (heghtScroll in 1..oneHeight){
            Log.e("tag","$height******第一部分0~300")
            topView.setBackgroundColor(resources.getColor(R.color.colorPrimary))
        }else if (heghtScroll in (oneHeight+1) ..  (oneHeight+twoHeight)){
            Log.e("tag","$height******第二部分300~600")
            topView.setBackgroundColor(resources.getColor(R.color.colorPrimaryDark))
        }else if (heghtScroll in (oneHeight+twoHeight+1) until (oneHeight+twoHeight+threeHeight)){
            Log.e("tag","$height******第三部分600~900")
            topView.setBackgroundColor(resources.getColor(android.R.color.holo_green_dark))
        }else if (heghtScroll == oneHeight+twoHeight+threeHeight){
            Log.e("tag","$height******第四部分900以上")
            topView.setBackgroundColor(resources.getColor(android.R.color.holo_purple))
        }else{
            Log.e("tag","******$height******")
        }
    }

根据滑动来改变状态栏的颜色(项目需求)
效果图:


a38767dd1db315b16f714f49296e624c.gif
上一篇 下一篇

猜你喜欢

热点阅读