一、继承AppBarLayout.Behavior
AppBarLayout有一个默认的Behavior,即AppBarLayout.Behavior,AppBarLayout.Behavior已注解的方式设置给AppBarLayout。
@CoordinatorLayout.DefaultBehavior(AppBarLayout.Behavior.class)
public class AppBarLayout extends LinearLayout {
...
}
1.继承AppBarLayout.Behavior自定义Behavior
我们可以继承AppBarLayout.Behavior并重新设置给AppBarLayout来修改AppBarLayout的默认滚动行为,实现AppBarLayout的弹性越界效果就可以通过这种方式实现。
继承AppBarLayout.Behavior需要重写构造方法
public class AppBarLayoutOverScrollViewBehavior extends AppBarLayout.Behavior {
public AppBarLayoutOverScrollViewBehavior() {
}
public AppBarLayoutOverScrollViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
2.将自定义的Behavior设置给AppBarLayout
可以通过两种方式将自定义的Behavior设置给AppBarLayout
在布局文件中设置
<android.support.design.widget.AppBarLayout ... app:layout_behavior="packageName.AppBarLayoutOverScrollViewBehavior"> </android.support.design.widget.AppBarLayout>
在代码中设置
AppBarLayout appBar = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setBehavior(new AppBarLayoutOverScrollViewBehavior());
appBar.setLayoutParams(params);
设置完成后,自定义的Behavior就会生效,但是因为没有重写任何方法,所以AppBarLayout的滚动行为不会发生变化。
二、Behavior中的回调方法分析
将自定义的Behavior设置给AppBarLayout后,可以在自定义的Behavior中重写滚动相关回调方法
public class AppBarLayoutOverScrollViewBehavior extends AppBarLayout.Behavior {
...
/**
* AppBarLayout布局时调用
*
* @param parent 父布局CoordinatorLayout
* @param abl 使用此Behavior的AppBarLayout
* @param layoutDirection 布局方向
* @return 返回true表示子View重新布局,返回false表示请求默认布局
*/
@Override
public boolean onLayoutChild(CoordinatorLayout parent, AppBarLayout abl, int layoutDirection) {
return super.onLayoutChild(parent, abl, layoutDirection);
}
/**
* 当CoordinatorLayout的子View尝试发起嵌套滚动时调用
*
* @param parent 父布局CoordinatorLayout
* @param child 使用此Behavior的AppBarLayout
* @param directTargetChild CoordinatorLayout的子View,或者是包含嵌套滚动操作的目标View
* @param target 发起嵌套滚动的目标View(即AppBarLayout下面的ScrollView或RecyclerView)
* @param nestedScrollAxes 嵌套滚动的方向
* @return 返回true表示接受滚动
*/
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes);
}
/**
* 当嵌套滚动已由CoordinatorLayout接受时调用
*
* @param coordinatorLayout 父布局CoordinatorLayout
* @param child 使用此Behavior的AppBarLayout
* @param directTargetChild CoordinatorLayout的子View,或者是包含嵌套滚动操作的目标View
* @param target 发起嵌套滚动的目标View(即AppBarLayout下面的ScrollView或RecyclerView)
* @param nestedScrollAxes 嵌套滚动的方向
*/
@Override
public void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
super.onNestedScrollAccepted(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
/**
* 当准备开始嵌套滚动时调用
*
* @param coordinatorLayout 父布局CoordinatorLayout
* @param child 使用此Behavior的AppBarLayout
* @param target 发起嵌套滚动的目标View(即AppBarLayout下面的ScrollVi