AppBarLayout源码解读

来源:互联网 发布:mac editplus 编辑:程序博客网 时间:2024/05/17 08:12

AppBarLayout源码解读

相关知识点

  • ActionBar&ToolBar&AppbarLayout
  • AppBarLayout作用
  • AppBarLayout&Behavior&CoordinatorLayout

核心内容

  1. Toolbar的出现是为了替代之前难用的ActionBar,而AppbarLayout的出现则是为了赋予Toolbar Material Design(以下简称MD)的属性,是Toolbar更灵活。
  2. AppbarLqyout是一个类似于LinearLayout的控件,要发挥出MD的特性还要依靠于v4包下的CoordinatorLayout,这个类提供了很Powerful的功能,主要的两个特点:一是他可以控制他的子布局之间的行为交互,二是可以根据CoordinatorLayout的手势操作,处理其任意子布局的行为及属性
  3. AppbarLayout与CoordinatorLayout的交互Google官方引进了Behavior这个概念,他定义了CoordinatorLayout布局内部的子控件与其他子布局以及父布局关联的Behavior。
  4. 结束了基础结构的介绍,来看源码肿么使用吧,要想实现AppbarLayout中Toolbar等子布局的MD特性,只有AppBarLayout做操作是实现不了的,还需要为Toolbar这样的子布局设置ScrollFlag这样的属性(Java或XML都有相应的设置属性),然后需要让AppBarLayout的最接近他的ViewGroup为CoordinatorLayout,并且AppBarLayout需要设置相应的监听手势的Behavior,可以通常情况下我们不需要这么做,这是为什么呢,因为Google大神通过注解的方式为想要遵循MD设置原则且被CoordinatorLayout包裹子布局提供了默认行为的注解,Look like this:@CoordinatorLayout.DefaultBehavior(AppBarLayout.Behavior.class) public class AppBarLayout extends LinearLayout5.完整的使用套路:
    * <android.support.design.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">**     <android.support.v4.widget.NestedScrollView*             android:layout_width="match_parent"*             android:layout_height="match_parent"*             app:layout_behavior="@string/appbar_scrolling_view_behavior">**         <!-- Your scrolling content -->**     </android.support.v4.widget.NestedScrollView>**     <android.support.design.widget.AppBarLayout*             android:layout_height="wrap_content"*             android:layout_width="match_parent">**         <android.support.v7.widget.Toolbar*                 ...*                 app:layout_scrollFlags="scroll|enterAlways"/>**         <android.support.design.widget.TabLayout*                 ...*                 app:layout_scrollFlags="scroll|enterAlways"/>**     </android.support.design.widget.AppBarLayout>** </android.support.design.widget.CoordinatorLayout>* 

那从上而下我们来看一下AppbarLayout的源码

  • 构造函数

1.为我们指出了使用此控件需要注意的基本问题,虽然AppbarLayout是类似于LinearLayout的控件,但是它目前子布局的排列方式(Orientation)只支持Vertical,但是在setOrientation中针对‘非Vertical’抛出了异常,可能以后会兼容水平方向的也说不定;

2.第二步在构造函数中检查了app主题的属性,其主题属性必须是Theme.AppCompat.Theme因为该控件主要是针对MD这种设计原则的,如果你当前应用的主题不是v7的兼容主题,他会抛出这样的异常:throw new IllegalArgumentException("You need to use a Theme.AppCompat theme " + "(or descendant) with the design library.");

3.在构造函数中我们还会看到很多针对21版本之后的操作,在21版本之后,为了达到MD的效果,Google大神为此控件添加了诸如动画,MD样式(elevation)和动画监听等特性。

  • LayoutParams类 1.该类主要作用是,为被AppbarLayout包裹的子控件的布局参数及特性,例如主要是相应于CoordinatorLayout内部其他控件的交互,从这个类的声明可以看出,为什么前面说的AppbarLayout类似LinearLayout了,代码贴出来自己感受一下:

    public static class LayoutParams extends LinearLayout.LayoutParams

该类剩余的部分大体看了一下除了ScrollFlag比较好玩还有个加速的的设置方法也是不错的,动态效果总能让app活灵活现,谁说不是呢。

  • Behavior类public static class Behavior extends HeaderBehavior<AppBarLayout>

1.这个类主要实现CoordinatorLayout子控件与其自身进行交互的Behavior处理 2.我们刚开始说了,要与CoordinatorLayout实现交互的控件,一般都需要实现Behavior而AppbarLayout默认提供了这样一个默认类来与CoordinatorLayout进行交互Behavior,则不需要在xml中配置,注意这种Behavior事AppbarLayout与CoordinatorLayout交互使用的,通常CoordinatorLayout将这种交互转移到可以滑动的子控件身上,但并不是所有的子控件都可以哦,目前常用的也就RecyclerView、NestedScrollView、ViewPager但是已经强大到爆了!!!

3.那如何使用呢,这种属性是设置在接受滚动事件的子控件上例如:


`<android.support.design.widget.FloatingActionButton    android:id="@+id/fab_for_changeview"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="bottom|right"    android:layout_margin="@dimen/float_action_button_margin"    android:src="@drawable/svg_icon_sign_white32"    app:backgroundTint="?attr/colorPrimary"    app:elevation="@dimen/float_action_button_elevation"    app:layout_anchorGravity="bottom|right|end"    app:layout_behavior="com.study.moodline.as.behaviors.ScrollAwareFABBehavior"    app:layout_scrollFlags="scroll|enterAlways" />`

4.但是要是AppbarLayout想与CoordinatorLayout里面的其他子控件(非可滚动的控件)交互怎么办呢,来看看下面这个类,仿照这个源码你可以想怎么玩就怎么玩哈哈哈哈哈,

  • ScrollingViewBehavior类 public static class ScrollingViewBehavior extends HeaderScrollingViewBehavior1.这个类主要的操作是CoordinatorLayout内部子控件与其他子控件滚动事件相互交互的功能 2.这个类里面重写的方法与上面的Behavior完全不一样,这些方法是位于CoordinatorLayout内容的方法,到最后我们发现,所有的行为其实都是通过继承CoordinatorLayout#Behavior发展而来的 3.具体的使用怎么用呢,这种行为是设置在可以执行滚动的View上的例如:

<com.study.moodline.as.selfweights.MySwipeRefreshLayout    android:id="@+id/swipe_refresh_fragment_home_page"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior">    <android.support.v7.widget.RecyclerView        android:id="@+id/recycler_view_fragment_home_page"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="vertical" /></com.study.moodline.as.selfweights.MySwipeRefreshLayout>     

总结

  • 使用AppbarLayout首先要注意内容控件Orientation的属性为Vertical,但不排除后期官方会修改,因为其内部计算子布局参数的方式是集成了LinearLayout.Params
  • 因为AppbarLayout目的是为了给Toolbar添加Material Design的特性,还要向下兼容所以注意你的主题需要使用Theme.AppCompat.Theme.XXX,详情参看v7
  • AppbarLayout与CoordinatorLayout连用,CoordinatorLayout是实现MD交互的重要类,其主要是通过自带的Behavior来实现MD的交互方式,针对于交互方式Google大神早已将子控件的Behavior划分为两类:
  • (1)一类是上面那种重写public interface NestedScrollingParent方法,实现与CoordinatorLayout的交互 
  • (2)另一类是重写CoordinatorLayout#layoutDependsOn/#onDependentViewChanged等方法就可以
1 0
原创粉丝点击