关于CoordinatorLayout与Behavior的一点分析

Behavior是Android新出的Design库里新增的布局概念。Behavior只有是CoordinatorLayout的直接子View才有意义。可以为任何View添加一个Behavior。
Behavior是一系列回调。让你有机会以非侵入的为View添加动态的依赖布局,和处理父布局(CoordinatorLayout)滑动手势的机会。不过官方只有少数几个Behavior的例子。对于理解Behavior实在不易。开发过程中也是很多坑,下面总结一下CoordinatorLayout与Behavior。

依赖

首先自定义一个Behavior。

    public class MyBehavior extends CoordinatorLayout.Behavior{
        public MyBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    }

一定要重写这个构造函数。因为CoordinatorLayout源码中parseBehavior()函数中直接反射调用这个构造函数。

static final Class<?>[] CONSTRUCTOR_PARAMS = new Class<?>[] {
        Context.class,
        AttributeSet.class
};

下面反射生成Behavior实例在实例化CoordinatorLayout.LayoutParams时:

final Class<Behavior> clazz = (Class<Behavior>) Class.forName(fullName, true,
                 context.getClassLoader());
c = clazz.getConstructor(CONSTRUCTOR_PARAMS);
c.setAccessible(true);
constructors.put(fullName, c);
return c.newInstance(context, attrs)

在任意View中添加:

app:layout_behavior=“你的Behavior包含包名的类名”

然后CoordinatorLayout就会反射生成你的Behavior。

另外一种方法如果你的自定义View默认使用一个Behavior。
在你的自定义View类上添加@DefaultBehavior(你的Behavior.class)这句注解。
你的View就默认使用这个Behavior。就像AppBarLayout一样。

@DefaultBehavior(AppBarLayout.Behavior.class)
public class AppBarLayout extends LinearLayout {}

生成Behavior后第一件事就是确定依赖关系。重写Behavior的这个方法来确定你依赖哪些View。

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
    return dependency.getId() == R.id.first;
}

child 是指应用behavior的View ,dependency 担任触发behavior的角色,并与child进行互动。
确定你是否依赖于这个View。CoordinatorLayout会将自己所有View遍历判断。
如果确定依赖。这个方法很重要。当所依赖的View变动时会回调这个方法。

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    return true;
}

下面这个例子:

    <declare-styleable name="Follow">
        <attr name="target" format="reference"/>
    </declare-styleable>

先自定义target这个属性。

  public class FollowBehavior extends CoordinatorLayout.Behavior {
  private int targetId;

  public FollowBehavior(Context context, AttributeSet attrs) {
      super(context, attrs);
      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Follow);
      for (int i = 0; i < a.getIndexCount(); i++) {
          int attr = a.getIndex(i);
          if(a.getIndex(i) == R.styleable.Follow_target){
              targetId = a.getResourceId(attr, -1);
          }
      }
      a.recycle();
  }

  @Override
  public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
       child.setY(dependency.getY()+dependency.getHeight());
      return true;
  }

  @Override
  public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
      return dependency.getId() == targetId;
  }
}

xml中:

<android.support.design.widget.CoordinatorLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <View
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        android:background="@android:color/holo_blue_light"/>

    <View
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        app:layout_behavior=".FollowBehavior"
        app:target="@id/first"
        android:background="@android:color/holo_green_light"/>


</android.support.design.widget.CoordinatorLayout>

效果是不管first怎么移动。second都会在他下面。


01.gif

滑动

Behavior最大的用处在于对滑动事件的处理。就像CollapsingToolbarLayout的那个酷炫效果一样。

主要是这3个方法,所依赖对象的滑动事件都将通知进来:

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
    return true;//这里返回true,才会接受到后续滑动事件。
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
//进行滑动事件处理
}

@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY, boolean consumed) {
//当进行快速滑动
    return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}

注意被依赖的View只有实现了NestedScrollingChild接口的才可以将事件传递给CoordinatorLayout。
但注意这个滑动事件是对于CoordinatorLayout的。所以只要CoordinatorLayout有NestedScrollingChild就会滑动,他滑动就会触发这几个回调。无论你是否依赖了那个View。
下面就是一个简单的View跟随ScrollView滑入滑出屏幕的例子。可以是Toolbar或其他任何View。

public class ScrollToTopBehavior extends CoordinatorLayout.Behavior<View>{
    int offsetTotal = 0;
    boolean scrolling = false;

    public ScrollToTopBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return true;
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        offset(child, dyConsumed);
    }

    public void offset(View child,int dy){
        int old = offsetTotal;
        int top = offsetTotal - dy;
        top = Math.max(top, -child.getHeight());
        top = Math.min(top, 0);
        offsetTotal = top;
        if (old == offsetTotal){
            scrolling = false;
            return;
        }
        int delta = offsetTotal-old;
        child.offsetTopAndBottom(delta);
        scrolling = true;
    }

}

xml中:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:context=".MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="128dp"
                style="@style/TextAppearance.AppCompat.Display3"
                android:text="A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z"
                android:background="@android:color/holo_red_light"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <View
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        app:layout_behavior=".ScrollToTopBehavior"
        android:background="@android:color/holo_blue_light"/>

</android.support.design.widget.CoordinatorLayout>

当NestedScrollView滑动的时候,first也能跟着滑动。toolbar和fab的上滑隐藏都可以这样实现。


02.gif

事件处理

这2个回调与View中的事件分发是一样的。所有Behavior能在子View之前收到CoordinatorLayout的所有触摸事件。可以进行拦截,如果拦截事件将不会流经子View。因为这2个方法都是在CoordinatorLayout的 回调中

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
    return super.onInterceptTouchEvent(parent, child, ev);
}

@Override
public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
    return super.onTouchEvent(parent, child, ev);
}

AppBarLayout的收缩原理分析

示例中给可滑动View设的Behavior是
@string/appbar_scrolling_view_behavior(android.support.design.widget.AppBarLayout$ScrollingViewBehavior)。
ScrollingViewBehavior的源码不多,看得出唯一的作用是把自己放到AppBarLayout的下面...(不能理解为什么叫ScrollingViewBehavior
所有View都能使用这个Behavior。

AppBarLayout自带一个Behivior。直接在源码里注解声明的。这个Behivior也只能用于AppBarLayout。
作用是让他根据CoordinatorLayout上的滚动手势进行一些效果(比如收缩)。与ScrollingViewBehavior是无关的,加不加ScrollingViewBehavior不影响收缩。
只不过只有某些可滑动View才会把滑动事件响应给CoordinatorLayout才能继而响应给AppBarLayout。



文/Jude95(简书作者)
原文链接:http://www.jianshu.com/p/a506ee4afecb
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文地址:https://www.cnblogs.com/Free-Thinker/p/6214006.html