MD控件(一) AppBarLayout

这次说的是AppBarLayout,因为项目需要吧,就拿过来用了,就此记录关于AppBarLayout自己理解的东西。

概述

AppBarLayout继承自LinearLayout,实现了许多材料设计的特性,换句话说,就是AppBarLayout中的控件可以很容易进行滑动控制。刚开始我也不是特别理解,也可能是自己的英文不好吧,andorid官方文档上面写成这样,那就默默往下看吧。
AppBarLayout可以与导航栏(Toolbar),标签栏(TabLayout)一起使用,从而达到MD风格中App Bar中的一些滚动设计效果。
这里写图片描述

上图的继承图可以看出,AppBarLayout是继承自LinearLayout,它为了实现交互动画效果增加了滑动特性,基本上严重依赖CoordinatorLayout.如果AppBarLayout在一个ViewGroup中,那么滑动特性将不起作用,此时使用AppBarLayout也将失去原本的意义.官网原话为:This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of it’s functionality will not work.

ScrollFlags

定义在AppBarLayout中的Children要明确设置各自的Scrolling behavior,也就是滑动行为.使用setScrollFlags()设置,XML文件可以使用android:layout_scrollflags 设置,存在5个常量进行区分.

scroll

文档说明:

The view will be scroll in direct relation to scroll events. This flag needs to be set for any of the other flags to take effect. If any sibling views before this one do not have this flag, then this value has no effect. 

位于AppBarLayout中的子View如果设置了android:layout_scrollFlags=’scroll’,那么该子view就可以随着滚动事件而滚动或者滚出屏幕了.但是需要注意的是,如果使用了其他值,scroll必须带上[使用 | 标志], 另外,即使本childView设置了scrollFlags=’scroll’事件,但是它的前面的任何其他View没有此标志位,本childView的设置将会无效.设置scroll代码如下:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="#00A999"
            app:layout_scrollFlags="scroll"/>
    </android.support.design.widget.AppBarLayout>

gif图如下:
这里写图片描述

enterAlways

文档:When entering (scrolling on screen) the view will scroll on any downwards scroll event, regardless of whether the scrolling view is also scrolling. This is commonly referred to as the ‘quick return’ pattern.

立足于快速放回模式,也就是返回时,向下滚动的scrollView[此时这个scrollView是与AppBarLayout同级的 可以滑动的View,可以为RecyclerView,NestedScrollView,但不可以是ListView/ScrollView,具体原因有时间再说吧]和 本chidView 滑动的优先级的问题.来看看 scroll 与scroll | enterAlways的区别吧:
这里写图片描述 这里写图片描述

看图中可以得出结论,向下滚动的scrollView会先滚动,完成之后,childView才会执行滚动事件;而scroll|enterAlways中childVIew会先执行滚动事件,完成之后scrollView才会滚动.

exitUntilCollapsed

文档说明: When exiting (scrolling off screen) the view will be scrolled until it is ‘collapsed’. The collapsed height is defined by the view’s minimum height.

也就是view会滚到最小高度处,然后scrolling view才去滚动,此时childView 不会消失[会附着在头顶吧 这个解释应该比较通俗了],
源码如下:

 <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="#00A999"
            android:minHeight="100dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"/>
    </android.support.design.widget.AppBarLayout>

gif图如下:
这里写图片描述

enterAlwaysCollapsed

文档: An additional flag for ‘enterAlways’ which modifies the returning view to only initially scroll back to it’s collapsed height. Once the scrolling view has reached the end of it’s scroll range, the remainder of this view will be scrolled into view. The collapsed height is defined by the view’s minimum height.

下滑时,childView先下滑到其最小高度minHeight,然后让scrollView滑动,滑动结束以后,childView由最小高度minHeight滑动到正常的height高度. 由于文档中说明,要实现这样的效果,是在enterAlways的属性上添加的,因为要这么设置:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="#00A999"
            android:minHeight="100dp"
            **app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"/>**
    </android.support.design.widget.AppBarLayout>

gif图为:
这里写图片描述

snap

文档: 404 notfound
这个最好理解了,你滑动松开时,childView要么height=0, 要么height=height,及childView要么不可见,要不都可见.
gif图如下:
这里写图片描述

基本上5个属性都说清楚了,有什么不对的大家指出啊.

AppBarLayout最重要的也就这么多了,还有什么需要记录的,以后会记录下来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值