編輯:關於 Android 程式設計
說到 Android 的機制,大家首先想到的多半是事件分發與事件攔截。但不知道你有沒有聽說過嵌套滑動機制?準確地說,可以把它理解為把事件分發與事件攔截綜合在了一起。
提到這個,第一個想到的應該是 CoordinatorLayout。只要自定義一個 Layout 並實作 NestedScrollingChild 接口,就能實現這些機制。但今天講的重點並不是 Child,而是 Parent。眾所周知,需要用到滾動機制的無非兩種:一是 ScrollView,二是列表。而現在的 ListView 早已被 RecyclerView 取代,RecyclerView 已經實作了 NestedScrollingChild 接口;關於 ScrollView,v4 包裡也有一個 NestedScrollView 實作了滑動機制的接口。
今天我們就透過觀察 CoordinatorLayout 的源碼來了解 NestedScrollingParent。
public interface NestedScrollingParent {
// 有嵌套滑動到來,詢問父 View 是否接受嵌套滑動
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);
// 父 View 接受嵌套滑動請求後調用(onStartNestedScroll 返回 true 時觸發)
public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);
// 停止嵌套滑動
public void onStopNestedScroll(View target);
// 子 View 滑動之後上報的滑動情況
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed);
// 子 View 滑動之前通知父 View 準備滑動的情況
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed);
// 子 View fling 之後上報的 fling 情況
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);
// 子 View fling 之前通知父 View 準備 fling 的情況
public boolean onNestedPreFling(View target, float velocityX, float velocityY);
// 獲取嵌套滑動的軸
public int getNestedScrollAxes();
}
執行過程大致為:在 onNestedScrollAccepted、onStopNestedScroll、getNestedScrollAxes 中呼叫 NestedScrollingParentHelper 對應的函數,再視情況在 onNestedScroll、onNestedPreScroll、onNestedFling、onNestedPreFling 中做相應處理。下面逐一來看:
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
boolean handled = false;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View view = getChildAt(i);
final LayoutParams lp = (LayoutParams) view.getLayoutParams();
final Behavior viewBehavior = lp.getBehavior();
if (viewBehavior != null) {
final boolean accepted = viewBehavior.onStartNestedScroll(this, view, child, target, nestedScrollAxes);
handled |= accepted;
lp.acceptNestedScroll(accepted);
} else {
lp.acceptNestedScroll(false);
}
}
return handled;
}
onStartNestedScroll 用來判斷父 View 是否參與滾動事件,源碼是透過 Behavior 以及遞歸調用來累計 handled。若我們自定義,可以在此直接 return true,告訴子 View「我會參與你的滾動事件」。
public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
mNestedScrollingParentHelper.onNestedScrollAccepted(child, target, nestedScrollAxes);
mNestedScrollingDirectChild = child;
mNestedScrollingTarget = target;
// 遍歷子 View,通知其 Behavior
}
onNestedScrollAccepted 是處理和子 View 一樣的滾動事件。若自定義,呼叫 helper.onNestedScrollAccepted(child, target, axes) 即可,只要參與了滾動事件,就需要處理滾動事件。
public void onStopNestedScroll(View target) {
mNestedScrollingParentHelper.onStopNestedScroll(target);
// 遍歷子 View,通知其 Behavior 停止滾動
mNestedScrollingDirectChild = null;
mNestedScrollingTarget = null;
}
onStopNestedScroll 用來讓 View 停止滾動,此時會通知子 View 停止滾動事件,相當於 action_up 的效果。
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
// 子 view 滑動結束後調用
// dyUnconsumed < 0 向下滾;dyUnconsumed > 0 向上滾
}
onNestedScroll 在子 View 滑動結束後調用。此時 view2 可能存在,但因為 view1 鋪滿整個螢幕而看不見它,如果我們想讓 view2 滑出來,只要當 view1 滾動結束,用此方法讓 view2 滾動出來即可。

public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
int xConsumed = 0;
int yConsumed = 0;
// 遍歷子 View,累計 Behavior 消耗的距離
consumed[0] = xConsumed;
consumed[1] = yConsumed;
}
onNestedPreScroll 相當於在子 View 滾動之前讓父 View 先滾動。consumed[0] 和 consumed[1] 是用來告訴子 View 父 View 在 x、y 方向滾動的偏移量,讓子 View 一起做出調整。這裡要特別注意:如果都返回 0,效果會與我們期望的相反。

我們想要的效果是上圖左邊那樣,而如果你都給它返回 0,就會變成右邊的效果,這並不是我們想要的。
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
boolean handled = false;
// 遍歷子 View,累計 Behavior 對 fling 的處理
return handled;
}
public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
boolean handled = false;
// 遍歷子 View,累計 Behavior 對 pre-fling 的處理
return handled;
}
public int getNestedScrollAxes() {
return mNestedScrollingParentHelper.getNestedScrollAxes();
}
onNestedFling 與 onNestedPreFling 分別對應子 View fling 之後與之前的回調,若不需要處理可直接 return false。getNestedScrollAxes 則直接返回 helper 中的軸資訊即可。
透過閱讀源碼,我們了解了 CoordinatorLayout 的執行流程。其實自定義 Layout 來實作這個接口,反而比 CoordinatorLayout 簡單很多。從源碼與註釋來看,我們的核心代碼主要寫在 onNestedPreScroll() 和 onNestedScroll() 這兩個方法裡,因為它們分別對應子 View 滾動之前與之後的時機。
現在,我們完全可以透過實作這個接口來自定義屬於自己的 CoordinatorLayout。自己實作可能只需要 200 多行代碼,而原生的接近 3000 行,且不一定符合自己的效果。如果你了解了滑動機制,就去實作屬於自己的 CoordinatorLayout 吧!
Android GPS 應用程式設計完整指南
GPS 簡介 GPS 是英文 Global Positioning System(全球定位系統)的簡稱,是 20 世紀 70 年代由美國陸海空三軍聯合研制的新一代空間衛星
LinearLayout 與 RelativeLayout 組合完成精細版面配置
padding 與 margin 的差異padding 是控制項內容與控制項邊緣之間的距離;margin 則是控制項邊緣與父容器之間的距離。兩者都用來處理間距,但參照對象
自訂 EditText 下劃線實戰:可變色底線、狀態圖示與靈敏刪除鈕的兩版演進
做過某個專案後,我對它的登入介面印象特別深刻。互動設計師給了一份非常「作」的規格:一個能依狀態變色的下劃線、左側會換圖的圖示、右側還要有會變色的刪除標誌。如下圖: 第
Android ViewPager 切換頁與游標動畫範例
ViewPager 適合用於實作多頁面的左右滑動切換效果。以下範例以三個頁面為基礎,搭配頂部標籤與游標圖片,展示如何讓頁面切換時,游標能依目前位置移動。布局與 Adapt