編輯:關於 Android 程式設計
最近專案碰到一個 ListView 的需求:向上滑動時隱藏 Header,向下滑動時迅速把 Header 顯示出來。
在 GitHub 上找到了 QuickReturnHeader 這個專案。雖然最後因為我們的特殊需求沒有採用它,但覺得這個實作思路不錯,於是花了點時間分析。
用法非常簡單:把 master 分支下載下來,將 library 目錄導入 Eclipse(當時用的 IDE),其他專案以 library 方式引用即可。
在 Activity 的 onCreate() 中:
QuickReturnHeaderHelper helper =
new QuickReturnHeaderHelper(this, R.layout.activity_listview, R.layout.header);
View view = helper.createView();
setContentView(view);
其中 R.layout.activity_listview 是 ListView 的版面配置:
<ListView
android:id="@android:id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="https://schemas.android.com/apk/res/android" />
這裡有個值得注意的點:專案使用的是系統預設的 ListView id @android:id/list,而且 QuickReturnHeader 的原始碼中把這個 id 寫死了。這是這個專案明顯需要改進的地方——一旦你的 ListView 用了自訂 id,它就抓不到控制項。
R.layout.header 則是 Header 的版面配置,範例如下:
<FrameLayout
android:background="@color/abs__background_holo_light"
android:layout_height="@dimen/abs__action_bar_default_height"
android:layout_width="match_parent"
xmlns:android="https://schemas.android.com/apk/res/android" >
<LinearLayout
android:id="@+id/frame"
android:layout_gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
<ImageButton
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:src="@drawable/ic_navigation_accept" />
<ImageButton
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:src="@drawable/ic_navigation_cancel" />
<ImageButton
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:src="@drawable/ic_navigation_refresh" />
<ImageButton
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:src="@drawable/ic_action_help" />
</LinearLayout>
<View
android:background="#3333"
android:layout_gravity="bottom"
android:layout_height="1dp"
android:layout_width="match_parent" />
</FrameLayout>
一句話總結:QuickReturnHeaderHelper 把 ListView 的版面配置和 Header 的版面配置一起放進一個 FrameLayout 中,同時幫 ListView 補上一個與 Header 等高的空白 HeaderView。

也就是說,畫面上看起來「Header 滑出去又不佔位」,靠的是兩個分身:一個是真的 Header,浮在上層隨捲動位移;另一個是透明的假 Header,掛在 ListView 內部佔住原本的高度,讓列表內容不會在 Header 消失後突然向上跳。
關鍵程式碼如下。建構子:
public QuickReturnHeaderHelper(Context context, int contentResId, int headerResId) {
this.context = context;
this.contentResId = contentResId;
this.headerResId = headerResId;
}
createView():
public View createView() {
inflater = LayoutInflater.from(context);
content = inflater.inflate(contentResId, null);
realHeader = inflater.inflate(headerResId, null);
realHeaderLayoutParams = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
realHeaderLayoutParams.gravity = Gravity.TOP;
// 先用量測結果估計 Header 高度,版面配置完成後會再取實際高度
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY);
realHeader.measure(widthMeasureSpec, heightMeasureSpec);
headerHeight = realHeader.getMeasuredHeight();
listView = (ListView) content.findViewById(android.R.id.list);
if (listView != null) {
createListView();
} else {
createScrollView();
}
return root;
}
createListView()(這個庫同樣支援 ScrollView,走 createScrollView(),程式碼類似):
private void createListView() {
root = (FrameLayout) inflater.inflate(R.layout.qrh__listview_container, null);
root.addView(content);
listView.getViewTreeObserver().addOnGlobalLayoutListener(this);
ListViewScrollObserver observer = new ListViewScrollObserver(listView);
observer.setOnScrollUpAndDownListener(new OnListViewScrollListener() {
@Override
public void onScrollUpDownChanged(int delta, int scrollPosition, boolean exact) {
onNewScroll(delta);
snap(headerTop == scrollPosition);
}
@Override
public void onScrollIdle() {
QuickReturnHeaderHelper.this.onScrollIdle();
}
});
root.addView(realHeader, realHeaderLayoutParams);
// 建立與 Header 等高的空白 HeaderView,佔住版面空間
dummyHeader = new View(context);
AbsListView.LayoutParams params =
new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, headerHeight);
dummyHeader.setLayoutParams(params);
listView.addHeaderView(dummyHeader);
}
配置就位之後,剩下就是監聽 ListView 的捲動事件:向上滑就逐步把自訂 Header 移出視線,向下滑就把它拉回來。onScrollIdle() 則負責在滑動停止後做「吸附」處理,讓 Header 停在完全顯示或完全隱藏的其中一態,不會卡在半吊子的位置。
整體看過原始碼後,原理其實一讀就懂,没什么玄機。這個專案仍有不少可改進之處,也未必適合每種需求——很多時候我們自訂的 ListView 已經在用 addHeaderView() 放了自己的 Header,這時再塞一個空白 Header 進去就會互相打架。
但它提供了一個可以借鏡的思路:把「浮動層」與「佔位層」拆成兩個物件,再靠捲動位移把兩者視覺上縫合起來。這個技巧在實作各種「滾動時收起的工具列」時都很通用,也正是後來的 CoordinatorLayout 與 AppBarLayout 在官方層級正式解決的問題。
專案效果展示:

用 LayoutParams 在代碼中動態控制控件位置與尺寸
XML 布局能解決的問題不要用代碼去做,但有些場景——比如尺寸和位置來自服務端下發的配置——就必須在運行時動態設置。這時候 LayoutParams 是唯一的工具。 L
Android 自訂軟鍵盤:Keyboard 與 KeyboardView 完整實作教學
翻資料時無意間發現了 android.inputmethodservice.Keyboard 這個類,原來 Android 早就內建了自訂鍵盤的能力,不需要自己拿一堆 B
Activity 與 Fragment 生命週期對照圖說
在閱讀一些開源專案時看到這張圖,覺得整理得非常好,就收藏下來了。 既然留了這篇,順手把圖上要記的重點用文字複習一遍。 Activity 的三個階段 生命週期可以歸
Android 開發藝術:第二章 IPC 機制與 Binder 通信全解析
2.1 IPC 是什麼 IPC 是進程間通信的縮寫。Android 基於 Linux,因此可使用共享文件、Socket、ContentProvider、Messenger