編輯:關於 Android 開發
像聯繫人那樣的分組懸浮列表,是很多應用常見的需求。本文透過自定義 PinnedHeaderListView,實現一個具備分組懸浮效果的列表。

(關於 gif 的製作:先錄下手機螢幕得到 mp4,再用 https://cloudconvert.com/mp4-to-gif 轉換為 gif,使用時記得選擇合適的尺寸,否則生成的檔案會過大。)
效果包含以下幾個方面:
1. 當某組的標題從螢幕頂部消失、而該組仍有成員在螢幕內時,組標題懸浮於頂部並顯示為紅色背景(本例中組標題原本是黃色背景)。
2. 當下一組的標題滑到頂部、與紅色懸浮標題相接時,把紅色標題頂走(懸浮標題隨下一組標題上滑直至消失),最終懸浮標題被替換。
3. 點擊 header view、組標題、組內元素、footer view 分別觸發各自的點擊事件。
取得當前應懸浮的標題 view,呼叫 ListView 的 dispatchDraw 方法,在頂部繪製懸浮標題:
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (currentSectionHeader != null) {
canvas.save();
canvas.translate(0, currentSectionHeaderOffest);
canvas.clipRect(0, 0, currentSectionHeader.width, currentSectionHeader.height);
currentSectionHeader.view.draw(canvas);
canvas.restore();
}
}
透過改變變量 currentSectionHeaderOffest 來移動懸浮標題的位置:
if (nextIsSectionHeaderView.getTop() >= 0 && currentSectionHeader != null
&& nextIsSectionHeaderView.getTop() < currentSectionHeader.height) {
currentSectionHeaderOffest = nextIsSectionHeaderView.getTop() - currentSectionHeader.height; // <0
Log.d(TAG, "move current section header view");
}
invalidate();
遍歷所有可見的 view,找到第一個組標題的位置與組號,再用 adapter.getView(…) 取得這個 view:
View nextIsSectionHeaderView = null;
for (int i = firstVisibleItem; i < firstVisibleItem + visibleItemCount; i++) {
if (mAdapter.isSectionHeader(i)) {
nextIsSectionHeaderView = getChildAt(i - firstVisibleItem);
nextSectionHeader = new SectionHeader(mAdapter.getView(i, null, this), i, mAdapter.getSectionId(i));
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
currentSectionHeaderOffest = 0;
headerCount = getHeaderViewsCount();
if (mAdapter == null || mAdapter.getCount() <= 0 || firstVisibleItem < headerCount) {
currentSectionHeader = null;
nextSectionHeader = null;
return;
}
firstVisibleItem = firstVisibleItem - headerCount;
// 遍歷所有可見部分找到 nextSectionHeader
View nextIsSectionHeaderView = null;
for (int i = firstVisibleItem; i < firstVisibleItem + visibleItemCount; i++) {
if (mAdapter.isSectionHeader(i)) {
nextIsSectionHeaderView = getChildAt(i - firstVisibleItem);
nextSectionHeader = new SectionHeader(mAdapter.getView(i, null, this), i, mAdapter.getSectionId(i));
break;
}
}
// 得到 currentSectionHeader
currentSectionHeader = new SectionHeader(
mAdapter.getSectionHeaderView(mAdapter.getSectionId(firstVisibleItem), null, this),
mAdapter.getSectionPosition(mAdapter.getSectionId(firstVisibleItem)),
mAdapter.getSectionId(firstVisibleItem));
currentSectionHeader.view.layout(0, 0, currentSectionHeader.width, currentSectionHeader.height); // 一定要有
// 判斷 nextSectionHeader 位置,若距頂部高度小於 currentSectionHeader 高度,則向上推移
if (nextIsSectionHeaderView != null) {
if (nextIsSectionHeaderView.getTop() >= 0 && currentSectionHeader != null
&& nextIsSectionHeaderView.getTop() < currentSectionHeader.height) {
currentSectionHeaderOffest = nextIsSectionHeaderView.getTop() - currentSectionHeader.height;
Log.d(TAG, "move current section header view");
}
invalidate();
}
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
headerCount = getHeaderViewsCount();
if (position < headerCount) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onHeaderClick(parent, view, position, id);
}
return;
}
if (mAdapter != null && position >= headerCount + mAdapter.getCount()) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onFooterClick(parent, view, position - headerCount - mAdapter.getCount(), id);
}
return;
}
position = position - headerCount;
int section = mAdapter.getSectionId(position);
int positionInSection = mAdapter.getPositionIdInSection(position);
if (mAdapter.isSectionHeader(position)) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onSectionClick(parent, view, section, id);
}
} else {
if (mOnItemClickListener != null) {
mOnItemClickListener.onSectionItemClick(parent, view, section, positionInSection, id);
}
}
}
完整源碼包含 CustomPinnedHeaderListView.java(自定義列表視圖)、CustomPinnedHeaderListViewBaseAdapter.java(抽象基類適配器)、CustomPinnedHeaderListViewAdapter.java(具體適配器實作)與 CustomPinnedHeaderListViewTestActivity.java(測試頁面)四個檔案。核心思路是透過 dispatchDraw 繪製懸浮標題、透過 onScroll 追蹤組標題位置、並以 offset 變量實現標題被頂走的動畫效果。掌握這套思路,就能靈活實現各種分組懸浮列表。
《Android 源碼設計模式解析與實戰》讀後札記(二十三):外觀模式 Facade
《Android 源碼設計模式解析與實戰》讀後札記(二十三):外觀模式 Facade 第二十三章、外觀模式 外觀模式是結構型設計模式之一,在實際開發中的出現頻率非常高,
Android 開發實戰(二十三):應用程式屏幕鎖定機制詳解
由於先前寫過屏幕鎖相關的文章,最近開發時又剛好用到,趁今天有空,就把屏幕鎖的控制與實現代碼重新複習、再深入學習一遍,希望能幫大家更了解屏幕鎖,以備不時之需。至於九宮格、P
Android View 分區點擊實作(一):以座標範圍判斷點擊區域
引言 好久沒更新 Android 相關文章了,社群裡常有朋友私訊催更,希望我專心寫 Android 進階內容。我理解大家的心情,但手上事情實在不少:除了公司專案,前陣子
FFmpeg 常用命令與轉碼參數入門
常用查詢命令 ffmpeg 是常見的音視訊轉碼工具。透過 ffmpeg -help 可查看基礎說明,透過 -help long 可查看進階參數,透過 -help full