android 顶部悬停折叠CoordinatorLayout,AppBarLayout

本文介绍了如何在Android中利用MaterialDesign5.0的CoordinatorLayout和AppBarLayout创建顶部悬停折叠效果。通过设置布局和使用特定属性,可以轻松实现滑动触发的折叠行为。

 

MaterialDesign5.0 中 CoordinatorLayout 和 AppBarLayout

来制作悬停效果布局 。

 

先看下栗子

 

 

实现很简单

 

<?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:orientation="vertical"
    tools:context="com.example.administrator.exampletest.MainActivity">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            >
            <!--
               appbarlayout 中  只要标记有
               app:layout_scrollFlags="scroll"
               在下部的监听滚动控件  上拉时会自动的收缩标记的内容
               如果不标记的  则会悬停在顶部
            -->

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_scrollFlags="scroll"
                >
                <ImageView
                    android:src="@mipmap/ic_launcher_round"
                    android:layout_width="wrap_content"
                    android:layout_height="25dp" />
                <ImageView
                    android:src="@mipmap/ic_launcher_round"
                    android:layout_width="wrap_content"
                    android:layout_height="25dp" />
                <ImageView
                    android:src="@mipmap/ic_launcher_round"
                    android:layout_width="wrap_content"
                    android:layout_height="25dp" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#f0f"
                app:layout_scrollFlags="scroll"
                ></View>


            <TextView
                android:background="#a3e2f6"
                android:text="我是悬停的那个控件哦"
                android:gravity="center"
                android:layout_margin="10dp"
                android:layout_width="match_parent"
                android:layout_height="20dp" />
        </android.support.design.widget.AppBarLayout>


        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >
            <!--
                需要监听滚动的控件
                app:layout_behavior="@string/appbar_scrolling_view_behavior"

             -->

        </android.support.v7.widget.RecyclerView>

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

 

布局文件使用

CoordinatorLayout

标明内部需要监听事件

 

 

 

AppBarLayout

 

标记需要折叠的布局 

在需要折叠布局中 标注

app:layout_scrollFlags="scroll"

 

需要在顶部停留的控件  就直接写就行了。

 

在需要监听滑动触发折叠的控件上 标注

app:layout_behavior="@string/appbar_scrolling_view_behavior"

 

简单实现就酱紫

 

小栗子

我是小栗子

 

 

 

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值