美文网首页
AppBarLayout

AppBarLayout

作者: 无语_ae67 | 来源:发表于2018-06-29 09:23 被阅读0次

    AppBarLayout 必须是CoordinatorLayout的子布局

    AppBarLayout 继承自LinearLayout,子控件默认为竖直方向显示,可以用它实现Material Design 的Toolbar;它支持滑动手势;它的子控件可以通过在代码里调用setScrollFlags(int)或者在XML里app:layout_scrollFlags来设置它的滑动手势。当然实现这些的前提是它的根布局必须是 CoordinatorLayout。这里的滑动手势可以理解为:当某个可滚动View的滚动手势发生变化时,AppBarLayout内部的子View实现某种动作。

    AppBarLayout的子控件不仅仅可以设置为Toolbar,也可以包含其他的View。

    在Toolbar上添加了一个app:layout_scrollFlags,并且把它的值设置为scroll|enterAlways 我们不说为什么我们先看一下实现的效果:

    AppBarAndRecycleForScroll.gif

    布局代码如下

     <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.support.v7.widget.Toolbar
                      android:id="@+id/tool"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
                      app:popupTheme="@style/Theme.AppCompat.Light"
    
                      app:layout_scrollFlags="scroll|enterAlways|snap">
            //设置AppBar滚动
                  </android.support.v7.widget.Toolbar>
              </android.support.design.widget.AppBarLayout>
      ​
              <android.support.v7.widget.RecyclerView
                  android:id="@+id/recy"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior"
                  //设置把自己放在AppBar之下,这是一个系统参数
                  ></android.support.v7.widget.RecyclerView>
      ​
      ​
          </android.support.design.widget.CoordinatorLayout>
    

    app:layout_scrollFlags共有五个值 最小值 android:minHeight="20dp"

    1. scroll 向上滚动时,Toobar向上滚动

    2. enterAlways 向下滚动时,Toobar向下滚动,并重新显示

    3. snap 当ToolBar还没有完全显示或隐藏时,会根据距离自动选则

    4. enterAlwaysCollapsed enterAlways的附加值。这里涉及到Child View的高度和最小高度,向下滚动时,Child View先向下滚动最小高度值,然后Scrolling View开始滚动,到达边界时,Child View再向下滚动,直至显示完全。 enterAlwaysCollapsed.gif
    5. exitUntilCollapsed 这里也涉及到最小高度。发生向上滚动事件时,Child View向上滚动退出直至最小高度,然后Scrolling View开始滚动。也就是,Child View不会完全退出屏幕。 exitUntilCollapsed.gif

    相关文章

      网友评论

          本文标题:AppBarLayout

          本文链接:https://www.haomeiwen.com/subject/qxmoyftx.html