Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自定義 PinnedHeaderListView:實現仿聯繫人分組懸浮列表

編輯:關於 Android 開發

自定義 PinnedHeaderListView:實現仿聯繫人分組懸浮列表


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

效果展示

(關於 gif 的製作:先錄下手機螢幕得到 mp4,再用 https://cloudconvert.com/mp4-to-gif 轉換為 gif,使用時記得選擇合適的尺寸,否則生成的檔案會過大。)

效果包含以下幾個方面:

1. 當某組的標題從螢幕頂部消失、而該組仍有成員在螢幕內時,組標題懸浮於頂部並顯示為紅色背景(本例中組標題原本是黃色背景)。

2. 當下一組的標題滑到頂部、與紅色懸浮標題相接時,把紅色標題頂走(懸浮標題隨下一組標題上滑直至消失),最終懸浮標題被替換。

3. 點擊 header view、組標題、組內元素、footer view 分別觸發各自的點擊事件。

實現思路

1. 懸浮 header 的實現

取得當前應懸浮的標題 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();
    }
}

2. 懸浮 header 被頂出螢幕的實現

透過改變變量 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();

3. 取得應懸浮於頂部的 view

遍歷所有可見的 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;
    }
}

4. 監聽 onScroll 時刻更新數據

@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();
    }
}

5. onItemClick 時處理 header 與 footer 的影響

@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 變量實現標題被頂走的動畫效果。掌握這套思路,就能靈活實現各種分組懸浮列表。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved