最新項目中用到了些Material效果,在此對自己的學(xué)習(xí)做個小結(jié)。
首先養(yǎng)成良好的學(xué)習(xí)習(xí)慣-----看源碼:
CoordinatorLayout
/**
* CoordinatorLayout is a super-powered {@link android.widget.FrameLayout FrameLayout}.
*
* <p>CoordinatorLayout is intended for two primary use cases:</p>
* <ol>
* <li>As a top-level application decor or chrome layout</li>
* <li>As a container for a specific interaction with one or more child views</li>
* </ol>
*
* <p>By specifying {@link CoordinatorLayout.Behavior Behaviors} for child views of a
* CoordinatorLayout you can provide many different interactions within a single parent and those
* views can also interact with one another. View classes can specify a default behavior when
* used as a child of a CoordinatorLayout using the
* {@link CoordinatorLayout.DefaultBehavior DefaultBehavior} annotation.</p>
*
* <p>Behaviors may be used to implement a variety of interactions and additional layout
* modifications ranging from sliding drawers and panels to swipe-dismissable elements and buttons
* that stick to other elements as they move and animate.</p>
*
* <p>Children of a CoordinatorLayout may have an
* {@link CoordinatorLayout.LayoutParams#setAnchorId(int) anchor}. This view id must correspond
* to an arbitrary descendant of the CoordinatorLayout, but it may not be the anchored child itself
* or a descendant of the anchored child. This can be used to place floating views relative to
* other arbitrary content panes.</p>
*/
public class CoordinatorLayout extends ViewGroup implements NestedScrollingParent {
大概的翻譯如下:
- CoordinateLayout是一個超級FrameLayout。
- CoordinateLayout原意是用于以下情況:
- 作為頂層布局。
- 作為一個容器,它的子View之間有特殊的相互作用(交互)。
- 通過為CoordinatorLayout的子views指定Behavior,在同一個父容器下可以提供不同的交互,并且那些子view可以和另一個子view相互作用,相互影響。通過@DefaultBehavior注釋,CoordinatorLayout的子view可以使用一個默認的behavior。
- Behavior可以用來實現(xiàn)各種交互和 來自滑動抽屜、滑動刪除元素和按鈕關(guān)聯(lián)其他元素產(chǎn)生的額外的布局修改。
- CoordinatorLayout的子view也許會有個anchor(錨點,即view顯示在哪塊區(qū)域),這個子view必須是CoordinatorLayout的直接子view,不能是孫子輩兒的view - -!
通過看CoordinatorLayout官方注釋文檔,了解到一個重要信息---Behavior,可以說CoordinatorLayout各種炫酷的交互效果,都是通過Behavior來控制的,除此之外,CoordinatorLayout類似于FrameLayout,那么接下來看重點:
Behavior:
/**
* Interaction behavior plugin for child views of {@link CoordinatorLayout}.
*
* <p>A Behavior implements one or more interactions that a user can take on a child view.
* These interactions may include drags, swipes, flings, or any other gestures.</p>
*
* @param <V> The View type that this Behavior operates on
*/
public static abstract class Behavior<V extends View> {
老規(guī)矩:
- CoordinatorLayout的子view的交互行為插件。
- 一個Behavior可以實現(xiàn)一個或更多的交互,用戶可以將這些交互用在CoordinatorLayout的子view上,這些交互包括:拖拽、滑動、飛速滑動、或者其他任何手勢。
基本使用:
新建一個HelloWorld,選擇BasicActivity,可以看到默認生成的布局中已經(jīng)包含了CoordinatorLayout,說明Goolge官方非常推薦使用這種布局。
官方給出的Behavior實現(xiàn)類有4個:
- BottomSheetBehavior
- SwipeDismissBehavior
- FloatingActionButton中實現(xiàn)的Behavior
- ViewOffsetBehavior

BottomSheetBehavior
一般用于底部彈出框,類似于微信支付的效果,使用方法如下:
xml布局
<android.support.design.widget.CoordinatorLayout
......
>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="@drawable/mv"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_peekHeight="0dp"
app:behavior_hideable="true">
<!--中間可以隨意寫自己的布局-->
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:layout_behavior="cn.lxf.behaviortext.FloatingBehavior"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Activity代碼
scrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(scrollView);
bottomSheetBehavior.setState(bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED?BottomSheetBehavior.STATE_EXPANDED:BottomSheetBehavior.STATE_COLLAPSED);
}
});
效果圖:

實現(xiàn)的關(guān)鍵點:
- 根布局為 CoordinatorLayout;
- 為底部彈出框設(shè)置app:layout_behavior="@string/bottom_sheet_behavior";
- 在代碼中得到BottomSheetBehavior,并動態(tài)改變它的狀態(tài);
- 至于FloatingActionButton跟隨彈出框移動,關(guān)鍵點在于為FloatingActionButton設(shè)置app:layout_behavior="cn.lxf.behaviortext.FloatingBehavior";這是一個自定義的Behavior,文章后面會有講解。
SwipeDismissBehavior
官方實現(xiàn)為Snackbar,已經(jīng)封裝好,唯一的條件是根布局必須為CoordinatorLayout,否則沒有效果。
??使用方式很簡單:
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
FloatingActionButton.Behavior
FloatingActionButton默認使用FloatingActionButton.Behavior,同Snackbar一樣,唯一需要注意的是根布局必須為CoordinatorLayout。
@CoordinatorLayout.DefaultBehavior(FloatingActionButton.Behavior.class)
public class FloatingActionButton extends VisibilityAwareImageButton {
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

ViewOffsetBehavior
官方實現(xiàn)為AppBarLayout中的Beihavior。

挑重點:
- AppBarLayout是一個實現(xiàn)了很多Material設(shè)計風(fēng)格的垂直的LinearLayout。
- 子view可以通過設(shè)置layout_scrollFlags來提供他們所需要的滾動方式。
- 你需要設(shè)置你的Scrolling view的behavior(app:layout_behavior)為AppBarLayout.ScrollingViewBehavior來提醒AppBarLayout什么時候滾動。
- 最下面為goolgle官方提供的一個例子。
話說第一次看到layout_scrollFlags屬性的萌新必然會一臉懵逼,到底應(yīng)該填什么呢(因為我當(dāng)初就是這樣...( ╯□╰ )),這里順便介紹ScrollFlags的幾個常量:
/**
* The view will be scroll in direct relation to scroll events. This flag needs to be
* set for any of the other flags to take effect. If any sibling views
* before this one do not have this flag, then this value has no effect.
*/
public static final int SCROLL_FLAG_SCROLL = 0x1;
/**
* When exiting (scrolling off screen) the view will be scrolled until it is
* 'collapsed'. The collapsed height is defined by the view's minimum height.
*
* @see ViewCompat#getMinimumHeight(View)
* @see View#setMinimumHeight(int)
*/
public static final int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED = 0x2;
/**
* When entering (scrolling on screen) the view will scroll on any downwards
* scroll event, regardless of whether the scrolling view is also scrolling. This
* is commonly referred to as the 'quick return' pattern.
*/
public static final int SCROLL_FLAG_ENTER_ALWAYS = 0x4;
/**
* An additional flag for 'enterAlways' which modifies the returning view to
* only initially scroll back to it's collapsed height. Once the scrolling view has
* reached the end of it's scroll range, the remainder of this view will be scrolled
* into view. The collapsed height is defined by the view's minimum height.
*
* @see ViewCompat#getMinimumHeight(View)
* @see View#setMinimumHeight(int)
*/
public static final int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED = 0x8;
/**
* Upon a scroll ending, if the view is only partially visible then it will be snapped
* and scrolled to it's closest edge. For example, if the view only has it's bottom 25%
* displayed, it will be scrolled off screen completely. Conversely, if it's bottom 75%
* is visible then it will be scrolled fully into view.
*/
public static final int SCROLL_FLAG_SNAP = 0x10;
- SCROLL_FLAG_SCROLL:對應(yīng)xml布局中的scroll,如果要設(shè)置其他的滾動flag,這個flag必須要設(shè)置,否則無效。
- SCROLL_FLAG_EXIT_UNTIL_COLLAPSED:對應(yīng)xml布局中的exitUntilCollapsed,設(shè)置該flag的view在向上滑動退出屏幕時,不會完全退出,會保留collapsed height(minHeight)高度。
- SCROLL_FLAG_ENTER_ALWAYS:對應(yīng)xml布局中的enterAlways,只要手指向下滑,設(shè)置該flag的view就會直接進入屏幕,不管NestedScrollView是否在滾動。
- SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED :對應(yīng)xml布局中的enterAlwaysCollapsed,是enterAlways的附加flag,使設(shè)置該flag的view在進入屏幕時最初只滑動顯示到它的collapsed height(minHeight),一旦NestedScrollView滑到頂部,該view再滑動顯示剩余的部分。
- SCROLL_FLAG_SNAP:對應(yīng)xml布局中的snap,設(shè)置該flag的view在滾動停止時,如果沒有完全顯示,會自動滾到到最近的一個邊界(頂端、中線和下線)。





自定義Behavior
自定義Behavior一般有兩種情況:
- 某個view監(jiān)聽另一個view的狀態(tài)變化,例如大小、位置、顯示狀態(tài)等。
- 某個view監(jiān)聽CoordinatorLayout里的滑動狀態(tài)。
上面這兩句話出自亓斌大神博客,然后我自己在學(xué)習(xí)過程中做了些實踐。
??首先看第一種,其實就是上面那個例子,F(xiàn)loatingActionButton怎么跟隨自己的布局上下移動?很明顯,這個效果類似于系統(tǒng)提供的FloatingActionButton+Snackbar的效果,看一下FloatingActionButton.Behavior源碼(只貼了關(guān)鍵部分):
/**
* Determine whether the supplied child view has another specific sibling view as a
* layout dependency.
*
* <p>This method will be called at least once in response to a layout request. If it
* returns true for a given child and dependency view pair, the parent CoordinatorLayout
* will:</p>
* <ol>
* <li>Always lay out this child after the dependent child is laid out, regardless
* of child order.</li>
* <li>Call {@link #onDependentViewChanged} when the dependency view's layout or
* position changes.</li>
* </ol>
*
* @param parent the parent view of the given child
* @param child the child view to test
* @param dependency the proposed dependency of child
* @return true if child's layout depends on the proposed dependency's layout,
* false otherwise
*
* @see #onDependentViewChanged(CoordinatorLayout, android.view.View, android.view.View)
*/
@Override
public boolean layoutDependsOn(CoordinatorLayout parent,
FloatingActionButton child, View dependency) {
// We're dependent on all SnackbarLayouts (if enabled)
return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child,
View dependency) {
if (dependency instanceof Snackbar.SnackbarLayout) {
updateFabTranslationForSnackbar(parent, child, dependency);
} else if (dependency instanceof AppBarLayout) {
// If we're depending on an AppBarLayout we will show/hide it automatically
// if the FAB is anchored to the AppBarLayout
updateFabVisibility(parent, (AppBarLayout) dependency, child);
}
return false;
}
- layoutDependsOn(parent,child, dependency):決定一個child是否有一個其他特殊的兄弟view(dependency)作為布局依賴,聽來比較拗口,其實就是指一個child是否會根據(jù)另一個view的參數(shù)和狀態(tài)而改變。這個方法在初始化布局時最少會被調(diào)用一次,如果返回true,則父布局(CoordinatorLayout)會不斷的調(diào)用onDependentViewChanged(parent,child, dependency)來改變該child的參數(shù)和狀態(tài)。
- 然后系統(tǒng)會在onDependentViewChanged中判斷dependency的類型,進而改變Fab的參數(shù)、狀態(tài)。
下面我們來比葫蘆畫瓢:
public class FloatingBehavior extends FloatingActionButton.Behavior {
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
return super.layoutDependsOn(parent, child, dependency) || dependency instanceof NestedScrollView;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
if (dependency instanceof NestedScrollView) {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
int fab_BM = lp.bottomMargin;
int distance = fab.getHeight() + fab_BM;
fab.setY(dependency.getY() - distance);
}
return super.onDependentViewChanged(parent, fab, dependency);
}
}
為Fab設(shè)置好改behavior后直接運行,發(fā)現(xiàn)報錯:Could not inflate Behavior subclass cn.lxf.behaviortext.FloatingBehavior,不能實例化該behavior。一番求證后發(fā)現(xiàn),自定義behavior時必須重寫那個帶2個參數(shù)的構(gòu)造方法,因為在CoordinatorLayout中是通過反射這個構(gòu)造方法來實例化behavior的,下面貼完整代碼:
public class FloatingBehavior extends FloatingActionButton.Behavior {
public FloatingBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
return super.layoutDependsOn(parent, child, dependency) || dependency instanceof NestedScrollView;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
if (dependency instanceof NestedScrollView) {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
int fab_BM = lp.bottomMargin;
int distance = fab.getHeight() + fab_BM ;
fab.setY(dependency.getY() - distance);
}
return super.onDependentViewChanged(parent, fab, dependency);
}
}
再看第二種情況,某個view監(jiān)聽CoordinateLayout中的滑動狀態(tài),這里最好先了解一下android Lollipop版本之后新增的嵌套滑動機制,推薦我學(xué)習(xí)過的一篇文章https://segmentfault.com/a/1190000002873657。
??對于監(jiān)聽滑動狀態(tài),我們需要關(guān)注的總共有以下幾個方法(由于篇幅原因,官方注釋被我干掉了,加了簡單的注解):
/**
* 當(dāng)一個CoordinatorLayout的子view準(zhǔn)備去開始一個嵌套滑動時調(diào)用
*
* @param coordinatorLayout 根布局coordinatorLayout
* @param child 這個behavior所關(guān)聯(lián)的coordinatorLayout的子view
* @param directTargetChild 包含target 嵌套滾動操作的coordinatorLayout子view
* @param target 開始嵌套滑動的coordinatorLayout子view。
* @param nestedScrollAxes 嵌套滾動的軸線。See
* {@link ViewCompat#SCROLL_AXIS_HORIZONTAL},
* {@link ViewCompat#SCROLL_AXIS_VERTICAL}
* @return 如果behavior希望接受這個嵌套滾動,則返回true。
*
* @see NestedScrollingParent#onStartNestedScroll(View, View, int)
*/
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
V child, View directTargetChild, View target, int nestedScrollAxes) {
return false;
}
/**
* 當(dāng)嵌套滑動已經(jīng)被CoordinatorLayout接受時調(diào)用。
*
* 參數(shù)同上。
* @see NestedScrollingParent#onNestedScrollAccepted(View, View, int)
*/
public void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, V child,
View directTargetChild, View target, int nestedScrollAxes) {
// Do nothing
}
/**
* 嵌套滾動結(jié)束時調(diào)用。
*
* 參數(shù)同上。
*
* @see NestedScrollingParent#onStopNestedScroll(View)
*/
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) {
// Do nothing
}
/**
* target滑動之后調(diào)用
*
* @param coordinatorLayout 同上
* @param child 同上
* @param target 同上
* @param dxConsumed 水平方向target滑動的距離(消耗的距離px),左滑大于0,右滑小于0
* @param dyConsumed 垂直方向target滑動的距離(消耗的距離px),上滑大于0,下滑小于0
* @param dxUnconsumed 水平方向target未滑動的距離(但是被用戶請求了)
* @param dyUnconsumed 垂直方向target未滑動的距離(但是被用戶請求了)
*
* @see NestedScrollingParent#onNestedScroll(View, int, int, int, int)
*/
public void onNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target,
int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
// Do nothing
}
/**
* target準(zhǔn)備滑動時(滑動之前)調(diào)用
*
* @param coordinatorLayout 同上
* @param child 同上
* @param target 同上
* @param dx 水平方向target想要滑動的距離(用戶請求的)
* @param dy 垂直方向target想要滑動的距離(用戶請求的)
* @param consumed 需要我們自己傳入。 consumed[0] 已經(jīng)被消費的水平方向滑動的距離, consumed[1] 已經(jīng)被消費的垂直方向滑動的距離
*
* @see NestedScrollingParent#onNestedPreScroll(View, int, int, int[])
*/
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target,
int dx, int dy, int[] consumed) {
// Do nothing
}
/**
* target在fling(飛速滾動)之后調(diào)用
*
* @param coordinatorLayout 同上
* @param child 同上
* @param target 同上
* @param velocityX 水平方向的速度
* @param velocityY 垂直方向的速度
* @param consumed child是否fling了
* @return behavior是否消費了這個fling
*
* @see NestedScrollingParent#onNestedFling(View, float, float, boolean)
*/
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, V child, View target,
float velocityX, float velocityY, boolean consumed) {
return false;
}
/**
* target在fling(飛速滾動)之前調(diào)用
*
* 參數(shù)同onNestedFling方法。
*
* @see NestedScrollingParent#onNestedPreFling(View, float, float)
*/
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target,
float velocityX, float velocityY) {
return false;
}
還是看上面那個FloatingActionButton的例子,改成上滑消失,下滑顯示,代碼如下:
public class FloatingBehavior extends FloatingActionButton.Behavior {
private boolean isAniming;
public FloatingBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
if (dyConsumed > 0 && !isAniming && child.getVisibility() == View.VISIBLE) {
hide(child);
} else if (dyConsumed < 0 && !isAniming && child.getVisibility() == View.INVISIBLE) {
show(child);
}
}
private void show(final View view){
ValueAnimator animator = ValueAnimator.ofFloat(0,1);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float f = (float) animation.getAnimatedValue();
view.setScaleX(f);
view.setScaleY(f);
}
});
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
view.setVisibility(View.VISIBLE);
isAniming = true;
}
@Override
public void onAnimationEnd(Animator animation) {
isAniming = false;
}
@Override
public void onAnimationCancel(Animator animation) {
isAniming = false;
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.start();
}
private void hide(final View view){
ValueAnimator animator = ValueAnimator.ofFloat(1,0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float f = (float) animation.getAnimatedValue();
view.setScaleX(f);
view.setScaleY(f);
}
});
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
isAniming = true;
}
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.INVISIBLE);
isAniming = false;
}
@Override
public void onAnimationCancel(Animator animation) {
isAniming = false;
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator.start();
}
}
效果圖:
