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都會在他下面。
滑動
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\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ"
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的上滑隱藏都可以這樣實現。
事件處理
這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。