目录
1.事件分发介绍
2.Down、up事件的分发过程
3.onTouchListener、onClickListener调用时机
4.事件拦截应用
5.NestedScrollingParent
6.Behavior的使用
7.NestedScrollingChild接口来源
NestedScrollingParent相关接口虽然能实现滑动的效果,但是每次我们都需要自定义View并实现相关接口,还是有些麻烦,因此Android又给出了Behavior供我们使用。使用时只需要先实现Behavior类(用于滑动事件的消费),然后在需要消耗的布局文件中声名,或者在类名前使用注解进行标注,就可实现,比之前的自定义View看起来更加简单,耦合更小,但是前提是你只能CoordinatorLayout中使用,下面就介绍下Behavior的使用及原理。
大家最熟悉的behavior使用场景应该是下面的
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout>
<ImageView />
</android.support.design.widget.CollapsingToolbarLayout>
<TextView />
</android.support.design.widget.AppBarLayout>
<com.example.irecyclerlib.IRecyclerView
app:layout_behavior="@stringappbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
但是Behavior到底是怎么起作用的呢,以及他都能实现什么样的功能呢?
下面先介绍下自定义behavior的实例,然后分析下它的原理。
本节实现以下效果:
首先看下Behavior的接口:
该动作分为两步,首先顶部TextView移动,然后RecyclerView跟随TextView移动,滑动到顶端后只有RecyclerView移动,下面介绍通过Behavior来实现。
behavior提供了两种功能:
1.view1监听另一个view2的状态变化(大小、位置、显示状态等),从而使自身也进行相应变化,由layoutDependsOn()、onDependentViewChanged()来实现该功能。
首先是RecyclerView的位置根据TextView的位置变化而变化。
public class RecyclerViewBehavior extends CoordinatorLayout.Behavior<RecyclerView> {
...
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, RecyclerView child, View dependency) {
//所以来的对象是TextView
return dependency instanceof TextView;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, RecyclerView child, View dependency) {
//计算列表y坐标,最小为0
float y = dependency.getHeight() + dependency.getTranslationY();
if (y < 0) {
y = 0;
}
child.setY(y);//recyclerView位置进行移动
return true;
}
}
2.实现onNestedScroll()等接口实现事件的传递,跟我们之前讲的接口NestedScrollingParent接口相似。
public class SampleHeaderBehavior extends CoordinatorLayout.Behavior<TextView> {
...
@Override
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull TextView textView, @NonNull View recyclerView, int dx, int dy,
@NonNull int[] consumed, int type) {
super.onNestedPreScroll(coordinatorLayout, textView, recyclerView, dx, dy, consumed, type);
if (recyclerView instanceof RecyclerView) {
//recyclerView是否可以滑动
if(canScroll(textView,dy,recyclerView)){
float finalY = textView.getTranslationY() - dy;//dy为移动分量
Log.d(TAG, "onNestedPreScroll: can finalY = "+finalY);
if (dy < -textView.getHeight()) {
dy = -textView.getHeight();
} else if (dy > textView.getHeight()) {
dy = textView.getHeight();
}
textView.setTranslationY(finalY);
consumed[1] = dy;
}
}
}
private boolean canScroll(View child ,int dy,View recyclerView){
if(dy > 0){
//上滑
Log.d(TAG, "canScroll->up:dy = "+dy+" getY = "+recyclerView.getY());
if(recyclerView.getY() > 0){
return true;
}else {
return false;
}
}else {
//下滑
Log.d(TAG, "canScroll->down:dy = "+dy+" getY = "+recyclerView.getY());
if(recyclerView.getY() < child.getHeight()){
return true;
}else {
return false;
}
}
}
}
1.下面介绍下layoutDependsOn实现原理:
他的原理很简单,就是在layoutDependsOn中设置依赖的对象(这里是TextView),然后TextView移动时,在onDependentViewChanged方法中移动child recyclerView。但是两个方法都是在什么地方调用的呢?
(1)首先是layoutDependsOn方法的调用
该方法在CoordinatorLayout的onMeasure方法中调用,然后存储依赖关系mDependencySortedChildren,供onDependentViewChanged使用
private void prepareChildren() {
...
for (int i = 0, count = getChildCount(); i < count; i++) {
final View view = getChildAt(i);
final LayoutParams lp = getResolvedLayoutParams(view);
lp.findAnchorView(this, view);
mChildDag.addNode(view);
// Now iterate again over the other children, adding any dependencies to the graph
for (int j = 0; j < count; j++) {
if (j == i) {
continue;
}
final View other = getChildAt(j);
//调用layoutParams的dependsOn方法,下面
if (lp.dependsOn(this, view, other)) {
if (!mChildDag.contains(other)) {
// Make sure that the other node is added
mChildDag.addNode(other);
}
// Now add the dependency to the graph
mChildDag.addEdge(other, view);
}
}
}
// Finally add the sorted graph list to our list
//然后将依赖关系进行存储
mDependencySortedChildren.addAll(mChildDag.getSortedList());
}
boolean dependsOn(CoordinatorLayout parent, View child, View dependency) {
//调用behavior的layoutDependsOn方法
return dependency == mAnchorDirectChild
|| shouldDodge(dependency, ViewCompat.getLayoutDirection(parent))
|| (mBehavior != null && mBehavior.layoutDependsOn(parent, child, dependency));
}
(2)onDependentViewChanged()的调用
该方法是在CoordinatorLayout类的onLayout方法中调用。
2.behavior的onNestedPreScroll()方法的调用
recyclerView滑动时,首先调用NestedScrollingChild2接口,通过NestedScrollingChildHelper传递到CoordinatorLayout的NestedScrollingParent2接口的onNestedPreScroll()方法,在该方法中再去调用behavior的onNestedPreScroll方法,由此最终将事件传递到behavior中进行消费。可以看出使用behavior也是在NestedScrollingParent、NestedScrollingChild接口的基础上多了一层调用,将滑动事件委托给behavior处理。
如果CoordinatorLayout与Behavior之间有其他布局,还能实现效果么?
从图上看出,CoordinatorLayout只会寻找他的一层子布局,不会再去深入获取,因此behavior不能被其他布局包裹。
上面分析完后,下面对CoordinatorLayout的经典组合进行简单分析调用过程:
1.NestedScrollView滑动时将Move事件传递给CoordinatorLayout,CoordinatorLayout将其传递给AppBarLayout的Behavior,调用其onNestedPreScroll方法进行AppBar的滑动。
2.AppBar滑动时,CoordinatorLayout调用其onLayout(),onLayout时获取view之间的依赖关系,然后将移动距离传递给ScrollingViewBehavior的onDependentViewChanged(),实现NestedScrollView跟随AppBar的移动。
代码详见 https://github.com/yanglele/AndroidSample/blob/master/app/src/main/java/com/example/yangl/androidsample/touchEvent/myCoordinatorLayout/MyCoordinatorActivity.java