Android新特性之CoordinatorLayout+AppBarLayout+RecyclerView实现下拉隐藏ToolBar
2016-04-10 11:59
801 查看
先上代码 :
要实现此效果 需要使用 CoorinatorLayout (协调布局) AppBarLayout(导航布局) + 一个可以滑动的布局(注意 不支持 listView和GirdView)
首先 要使用 CoorinatorLayout作为根布局 其中要包含 导航布局 和 滑动布局 导航布局中 包括需要隐藏的布局
滑动布局 必须设置
1) 首先需要用CoordinatorLayout包住AppBarLayout;
2) 顶部区域的View都放在AppBarLayout里面;
3) AppBarLayout外面,CoordinatorLayout里面,放一个带有可滚动的View.如上的例子,放的是ViewPager,而ViewPager里面是放了RecylerView的,即是可以滚动的View.
4) 在AppBarLayout里面的View,通过app:layout_scrollFlags属性来控制,滚动时候的表现.其中有4种Flag的类型.
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
this flag ensures that any downward scroll will cause this view to become visible, enabling the ‘quick return’ pattern
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.
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的逻辑就可以了.
此六步原文地址
http://www.w2bc.com/Article/49080
<?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:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" /> </android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv" app:layout_behavior="@string/appbar_scrolling_view_behavior" ></android.support.v7.widget.RecyclerView> </android.support.design.widget.CoordinatorLayout>
要实现此效果 需要使用 CoorinatorLayout (协调布局) AppBarLayout(导航布局) + 一个可以滑动的布局(注意 不支持 listView和GirdView)
首先 要使用 CoorinatorLayout作为根布局 其中要包含 导航布局 和 滑动布局 导航布局中 包括需要隐藏的布局
滑动布局 必须设置
app:layout_behavior="@string/appbar_scrolling_view_behavior"
//需要隐藏的布局必须需要设置
app:layout_scrollFlags="scroll|enterAlways" 属性
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的逻辑就可以了.
此六步原文地址
http://www.w2bc.com/Article/49080
相关文章推荐
- 如何将ADT项目导入Android studio及常見問題
- Xcode侧栏无法拖动以及无法选择模拟器的解决办法
- 史上最详细Android集成QQ,微信,微博分享(不用第三方)持续更新中
- android 技巧 疑难问题集合
- cocos2dx——戏如人生
- ios上视频与音乐合成后出现播放兼容问题的解决方法
- ios上视频与音乐合成后出现播放兼容问题的解决方法
- Android-SeekBar进度条的使用
- ANDROID垃圾回收器机制和强引用、软引用、弱引用、虚引用
- Android中weight 出现权重大但是所占的比例小问题
- android扫一扫 二维码显示结果中文乱码
- Swift学习笔记之基本数据类型-数组与字典
- Android中button setbackground setbackgroundColor方法问题
- LruCache详解之 Android 内存优化
- Android 5.0 新特性之RecyclerView
- iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客
- iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客
- iOS SDWebImage源码研究(二)
- Android Studio export heap dump
- Android开发工具: AS, Gradle, Git等