Android Behavior
原文出處:http://www.jianshu.com/p/a506ee4afecb
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一樣。
生成Behavior后第一件事就是確定依賴關系。重寫Behavior的這個方法來確定你依賴哪些View。
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變動時會回調這個方法。
下面這個例子:
<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();}public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {child.setY(dependency.getY()+dependency.getHeight());return true;}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"><Viewandroid:id="@+id/first"android:layout_width="match_parent"android:layout_height="128dp"android:background="@android:color/holo_blue_light"/><Viewandroid: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個方法,所依賴對象的滑動事件都將通知進來:
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {return true;//這里返回true,才會接受到后續滑動事件。 } public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { //進行滑動事件處理 } 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。
xml中:
<android.support.design.widget.CoordinatorLayoutxmlns: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.NestedScrollViewandroid:id="@+id/second"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid: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><Viewandroid: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的 回調中
public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {return super.onInterceptTouchEvent(parent, child, ev); } 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。
總結
以上是生活随笔為你收集整理的Android Behavior的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL基础系列(五)——子查询
- 下一篇: 软件工程7-软件测试