您的位置:首页 > 移动开发 > Android开发

两个Layout一个属性快速实现Android滑动顶部悬停

2016-12-22 13:31 295 查看
大家先来看一下这个效果吧,在项目中应用的场景还是蛮多的。



之前在网上看到有许多实现这个效果的做法,但是基本上是在滑动视图内改动,比如获取滑动的Y值来确定悬停View的位置。

在design包问世之前这种做法可能相对较好。

时代在变迁,一些沉重的东西当然也是要随时代而过去;

新的东西也会在乱世中浮现。

CoordinatorLayout

想必大家都已经知道这个Layout了。协调布局,主要是其子布局中设置的behavior。

behavior是在监听一个View的行为而来做出相应的处理

AppBarLayout

它是一个vertical 的LinerLayout,具有design的app bar的特点,也就是滑动手势的处理。

它的Children 应提供一个滑动行为处理的标记,也就是在滑动时候,我该做什么。

当然,它还需要一个兄弟节点来提供滑动行为,也就是上面说的behavior.

app:layout_scrollFlags

这个属性就是说明在滑动的时候,我需要做什么的标记

scroll:滚动出屏幕

enterAlways:永远的进入,也就是向下滑的时候,会永远的出现它

enterAlwaysCollapsed: 这个跟上面的相似,只是在你设置了minHeight的时候,下滑时以最小高度进入,并且跟随滑动变大。

exitUntilCollapsed: 滚动出屏幕,并且折叠在顶部。

snap:卡在那里不动。默认值

通过上面的介绍,想必大家已经有点思路了。

直接上这段代码吧

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout    //第一个Layout
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="wrap_content">

<android.support.design.widget.AppBarLayout   //第二个Layout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@android:color/holo_orange_light"
android:orientation="vertical"
app:layout_scrollFlags="scroll" />     //一个属性

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_blue_light"
android:orientation="vertical" />
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>


结束语

两个Layout,一个属性,很快速实现这个效果,希望对大家有所帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: