CoordinatorLayout.Behavior
(CoordinatorLayout 与 AppbarLayout )
scroll:伴随整体一起上下滑动
enterAlways:只要一滑动就会出现
enterAlwaysCollapsed:一开始是enterAlways效果,之后会像弹簧一样返回到collapsed状态,官方是这么解释的
- 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.
*
(CoordinatorLayout 与 ToolBar)
(CoordinatorLayout 与 CollapsingLayout)
实现折叠效果,在折叠的过程中可以定义很多的属性,
Collapsing title:设置折叠的title,动态设置该title会影响Toolbar的Title的显示
Content scrim:对内容设置纱布 drawable
Status bar scrim:状态栏纱布 drawable, 这两个纱布都得是滑动到阈值时展现的
Parallax:比全局上下滑动要慢一些
Pinned:固定子view,其实在appbarlayout里面不设置scroll,一样可以实现固定
snap:view显示高度低于25%,直接隐藏;高于75%,直接显示
/**
* Upon a scroll ending, if the view is only partially visible then it will be snapped
* and scrolled to it’s closest edge. For example, if the view only has it’s bottom 25%
* displayed, it will be scrolled off screen completely. Conversely, if it’s bottom 75%
* is visible then it will be scrolled fully into view.
*/
(AppbarLayout、ToolBar、CollapsingLayout 三者关系)
(CoordinatorLayout 与 FloatActionButton)
(layoutDependsOn —- FloatActionButton 没有使用,但是由onDependentViewChanged关联??)
(思考默认layoutDependsOn 关联的哪个view)
//首先继承自CoordinatorLayout.Behavior,如:
public class DemoBehavior extends CoordinatorLayout.Behavior<TextView> {
//该构造函数必须添加,否则报错
public DemoBehavior(Context context, AttributeSet attrs) {
super(context, attrs);}
}
两个
//该函数关联的 dependency可以是多个view,比如有两个button
boolean layoutDependsOn(CoordiantorLayout parent, TextView child, View dependency) {
//用来寻找依赖的view,如
return dependency of Button; //那么同一级别的子view中, 所有Button的变化都会被检测到,从而与其关联起来
}
boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {
//根据 dependency 的变化, 修改child的属性,比如颜色、背景、位移等等
}
其它可以使用的方法:
onLayoutChild(CoordinatorLayout parent, TextView child, int layoutDirection) ; //根据需要重新布局CoordinatorLayout的子View,尤其是ToolBar、FloatingActionButton等原生的
- CoordinatorLayout 与AppBarLayout
- CoordinatorLayout与CollapseLayout
- CoordianatorLayout与CoordinatorLayout.LayoutParams
setBehavior()
//构造函数里直接设置LayoutParams
(CoordinatorLayout.LayoutParams p)
(ViewGroup.MarginLayoutParams p)
(ViewGroup.LayoutParams p)
4.ToolBar 放在不同位置处的影响:
放在AppBarLayout外面,一样会滑动,但是不会被隐藏掉