CoordinatorLayout初识

项目中有一个tab吸顶效果,最初的想法是最外层scrollview,里面写tab,滑动的时候获取tab距离scroll的Y轴顶点的距离,如果等于0的时候,就让tab的Y值一直为0.

思想图

实际开发的时候遇到了一个问题,因为我目前的水平不够,自定义scrollview的时候,获取不到其子控件的Y值.后来不得不放弃.

但是我又不想因为一个效果去专门引用一个第三方库,一次偶然的情况下知道了CoordinatorLayout,他是Google官方design库中的一个控件,详细了解之后发现他能实现和我项目中需求一样的效果.而且非常方便.

在用CoordinatorLayout,踩了点坑,最初我的布局是这样写的,感觉并没有毛病,但是运行起来之后却并不能滑动.

<?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:background="#2b93ff"
    android:orientation="vertical"
    tools:context="com.example.tab.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <Button
            android:id="@+id/choose_sex"
            android:layout_width="45px"
            android:layout_height="45px"
            android:background="@mipmap/home_filter"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textColor="#fff"
            android:textSize="16sp"
            android:layout_centerInParent="true"/>

        <Button
            android:id="@+id/select_user"
            android:layout_width="45px"
            android:layout_height="45px"
            android:background="@mipmap/home_search"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"
            android:layout_centerVertical="true"/>

    </RelativeLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:background="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
                    <ImageView
                        android:id="@+id/img_s_logo"
                        android:layout_width="match_parent"
                        android:layout_height="500px"
                        android:layout_gravity="center"
                        android:visibility="visible"
                        android:scaleType="centerCrop"
                        android:src="@mipmap/h"
                        />
                

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                app:tabIndicatorHeight="2dp"
                app:tabGravity="center"
                android:layout_height="45dp" >

                <android.support.design.widget.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="牛逼"/>
                <android.support.design.widget.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="吊炸天"/>

            </android.support.design.widget.TabLayout>
        </android.support.design.widget.AppBarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/testList"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

但是在我下载下来的demo里面却是可以正常滑动的.这里同学们需要注意一点.AppBarLayout虽然相当于是头布局,但是如果想要实现可滑动的折叠效果,就需要使用官方库中的一个控件:CollapsingToolbarLayout.

修改之后的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:background="#2b93ff"
    android:orientation="vertical"
    tools:context="com.example.tab.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <Button
            android:id="@+id/choose_sex"
            android:layout_width="45px"
            android:layout_height="45px"
            android:background="@mipmap/home_filter"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textColor="#fff"
            android:textSize="16sp"
            android:layout_centerInParent="true"/>

        <Button
            android:id="@+id/select_user"
            android:layout_width="45px"
            android:layout_height="45px"
            android:background="@mipmap/home_search"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"
            android:layout_centerVertical="true"/>

    </RelativeLayout>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:background="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:layout_height="wrap_content"
                app:contentScrim="#2b93ff"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
                <RelativeLayout
                    android:id="@+id/lin_all"
                    android:layout_width="match_parent"
                    android:layout_height="500px"
                    android:orientation="vertical"
                    app:layout_collapseParallaxMultiplier="0.6"
                    >
                    <ImageView
                        android:id="@+id/img_s_logo"
                        android:layout_width="match_parent"
                        android:layout_height="500px"
                        android:layout_gravity="center"
                        android:visibility="visible"
                        android:scaleType="centerCrop"
                        android:src="@mipmap/h"
                        />
                </RelativeLayout>

            </android.support.design.widget.CollapsingToolbarLayout>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                app:tabIndicatorHeight="2dp"
                app:tabGravity="center"
                android:layout_height="45dp" >

                <android.support.design.widget.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="牛逼"/>
                <android.support.design.widget.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="吊炸天"/>

            </android.support.design.widget.TabLayout>
        </android.support.design.widget.AppBarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/testList"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

现在我们直接运行起来就可以了,不用在Activity中设置.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WWGtest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值