Android CoordinatorLayout + AppBarLayout(向上滚动隐藏指定的View)
2015-12-23 21:00
477 查看
在新的Android Support Library里面,新增了CoordinatorLayout, AppBarLayout等.
实现的效果: 向下滚动RecylerView,Tab会被隐藏,向上滚动RecylerView,Tab恢复出现.这么做的好处在于,用户能有更多的空间位置去看列表里面的内容.
实现步骤:
1) 首先需要用CoordinatorLayout包住AppBarLayout;
2) 顶部区域的View都放在AppBarLayout里面;
3) AppBarLayout外面,CoordinatorLayout里面,放一个带有可滚动的View.如上的例子,放的是ViewPager,而ViewPager里面是放了RecylerView的,即是可以滚动的View.
4) 在AppBarLayout里面的View,通过app:layout_scrollFlags属性来控制,滚动时候的表现.其中有4种Flag的类型.
上面的例子种用的是 scroll 和 enterAlways.
Scroll 表示向下滚动时,这个View会被滚出屏幕范围直到隐藏.
enterAlways 表示向上滚动时,这个View会随着滚动手势出现,直到恢复原来的位置.
5) 在可以滚动的View上设置属性 app:layout_behavior.
该属性的值实际上是一个完整的class名字,而上面例子中的 @string/appbar_scrolling_view_behavior 是Android Support Library 定义后的值,可以被直接使用.
这个Behavior的class是真正控制滚动时候View的滚动行为.我们也可以继承Behavior这个class去实现特有的滚动行为.
6) 代码部分,只需要实现RecylerView的逻辑就可以了.
实现的效果: 向下滚动RecylerView,Tab会被隐藏,向上滚动RecylerView,Tab恢复出现.这么做的好处在于,用户能有更多的空间位置去看列表里面的内容.
实现步骤:
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/third_activity_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|enterAlways" app:tabIndicatorColor="@color/medium_blue" app:tabSelectedTextColor="@color/medium_blue" app:tabTextAppearance="@style/TabText" app:tabTextColor="@color/gray_text"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout>
1) 首先需要用CoordinatorLayout包住AppBarLayout;
2) 顶部区域的View都放在AppBarLayout里面;
3) AppBarLayout外面,CoordinatorLayout里面,放一个带有可滚动的View.如上的例子,放的是ViewPager,而ViewPager里面是放了RecylerView的,即是可以滚动的View.
4) 在AppBarLayout里面的View,通过app:layout_scrollFlags属性来控制,滚动时候的表现.其中有4种Flag的类型.
scroll: this flag should be set for all views that want to scroll off the screen - for views that do not use this flag, they’ll remain pinned to the top of the screen
enterAlways: this flag ensures that any downward scroll will cause this view to become visible, enabling the ‘quick return’ pattern
enterAlwaysCollapsed: When your view has declared a minHeight and you use this flag, your View will only enter at its minimum height (i.e., ‘collapsed’), only re-expanding to its full height when the scrolling view has reached it’s top.
exitUntilCollapsed: this flag causes the view to scroll off until it is ‘collapsed’ (its minHeight) before exiting
上面的例子种用的是 scroll 和 enterAlways.
Scroll 表示向下滚动时,这个View会被滚出屏幕范围直到隐藏.
enterAlways 表示向上滚动时,这个View会随着滚动手势出现,直到恢复原来的位置.
5) 在可以滚动的View上设置属性 app:layout_behavior.
该属性的值实际上是一个完整的class名字,而上面例子中的 @string/appbar_scrolling_view_behavior 是Android Support Library 定义后的值,可以被直接使用.
这个Behavior的class是真正控制滚动时候View的滚动行为.我们也可以继承Behavior这个class去实现特有的滚动行为.
6) 代码部分,只需要实现RecylerView的逻辑就可以了.
相关文章推荐
- Android Framework 概述笔记
- QQ ,微博,一键分享的步骤,详细到没朋友
- Android持久化存储SharedPreferences存储键值对的方式介绍
- 微信支付与微信应用间跳转
- Leetcode206: Trapping Rain Water
- Android setContentView源码解析
- Android实现数据持久化的文件存储
- Android开发之第一个小项目--我的MP3播放器之最后一点结束的话(终)
- 微信调试器
- iSensor APP 之 摄像头调试 OV9655
- Android 图片三级缓存之内存缓存(告别软引用(SoftRefrerence)和弱引用(WeakReference))
- Android的学习路线
- Android之图片拉伸处理
- 手机浏览器判断app是否安装并启动
- Android中的shape中的属性大全
- Android处理Bitmap的一些方法(多)
- swift锁屏播放,音乐进度更新,专辑,歌手名显示
- android属性动画(property animation)使用介绍
- 【android】EditText监听,根据输入内容查询数据并动态更新ListView
- android vebview与js交互 webview与网页交互