CoordinatorLayout 与Behavior

本文详细介绍了CoordinatorLayout及其与其他组件如AppBarLayout、ToolBar、CollapsingLayout的配合使用方式。包括scroll、enterAlways、enterAlwaysCollapsed等行为特性,以及如何通过设置Behavior来实现复杂交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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等原生的

  1. CoordinatorLayout 与AppBarLayout
  2. CoordinatorLayout与CollapseLayout
  3. CoordianatorLayout与CoordinatorLayout.LayoutParams
    setBehavior()
    //构造函数里直接设置LayoutParams
    (CoordinatorLayout.LayoutParams p)
    (ViewGroup.MarginLayoutParams p)
    (ViewGroup.LayoutParams p)
    4.ToolBar 放在不同位置处的影响:
    放在AppBarLayout外面,一样会滑动,但是不会被隐藏掉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值