編輯:關於 Android 程式設計
側滑刪除已經是列表頁的標配交互,我自己也一直想動手做一個類似 QQ 的版本。自定義 View 我摸過一段時間,但自定義 ViewGroup 的實戰經驗偏少,而側滑刪除偏偏最吃這塊——它要處理大量事件分發的細節:子控制項與父控制項的滑動衝突如何仲裁、down → move…move… → up 這一整串序列怎麼排布等等。
童哥寫過一個很不錯的側滑刪除,用法簡單,更關鍵的是它把解耦做得非常漂亮:無論你用的是 ListView、RecyclerView,還是其他 ViewGroup 裡的子 View,都能套用同一種寫法。使用方式只需一步——在 XML 佈局中,用這個自定義側滑類去包住需要側滑的 item。舉例來說,想讓 ListView 支援側滑刪除,只要在定義 item 佈局時,把自定義類作為 item 的根佈局就好。
既然輪子已經有了,為什麼還要再打造一遍?理由很單純:一是我的 ViewGroup 實戰偏少,二是想更深入了解事件分發機制,而童哥那個 demo 剛好覆蓋了我的知識盲區。自己跟著敲一遍,收穫遠大於只看。

isLeftSwipe 開關支援左滑與右滑。重寫 onMeasure() 告訴父控制項你需要多大尺寸,重寫 onLayout() 決定每個子 View 的位置。位置計算需要遍歷所有 child:
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
if (childView.getVisibility() != GONE) {
// 具體的擺放邏輯
}
}
先看 item 佈局,理解起來會更順。注意:用自定義 ViewGroup 包住佈局時,記得加上 android:clickable="true":
<com.example.swipedelete.view.SwipeMenuLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<!-- 第一個子 view:列表內容 -->
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/listview_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/listview_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="5dp"
android:text="我是 listview 的 item 內容" />
</LinearLayout>
<!-- 第二個之後的子 view:側滑菜單 -->
<Button
android:id="@+id/btn_zd"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#d9dee4"
android:text="置頂"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btn_delete"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#F76E6B"
android:text="刪除"
android:textColor="@android:color/white" />
</com.example.swipedelete.view.SwipeMenuLayout>
接著是 onLayout() 的擺放邏輯:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int left = 0 + getPaddingLeft();
int right = 0;
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
if (childView.getVisibility() != GONE) {
if (i == 0) {
// 第一個子 View 是內容區,寬度鋪滿
childView.layout(left, getPaddingTop(),
left + mMaxWidth, getPaddingTop() + childView.getMeasuredHeight());
left = left + mMaxWidth;
} else {
if (isLeftSwipe) {
childView.layout(left, getPaddingTop(),
left + childView.getMeasuredWidth(),
getPaddingTop() + childView.getMeasuredHeight());
left = left + childView.getMeasuredWidth();
} else {
childView.layout(right - childView.getMeasuredWidth(), getPaddingTop(),
right, getPaddingTop() + childView.getMeasuredHeight());
right = right - childView.getMeasuredWidth();
}
}
}
}
}
這裡的關鍵在於:childCount 是 3(內容、置頂、刪除)。當 i == 0 時拿到的是內容 View,寬度設為整屏,此時 left = getPaddingLeft(),沒設內邊距就是 0。當 i != 0 時進入菜單區,以左側滑為例:三塊是橫向排列的,即使菜單此刻還藏在螢幕外,第二個子 View 的 left 也必然等於「自身寬度 + 第一個子 View 寬度」,依此類推決定後續每一塊的累加位置。
做法是引入 scaleTouchSlop——系統定義的閾值,手指位移超過它就代表這是一次滑動而非點擊。用這個值來判斷當前是否處於側滑狀態,就能決定要不要吞掉點擊。
ViewGroup 的事件分發由三部分構成:dispatchTouchEvent()、onInterceptTouchEvent()、onTouchEvent(),也就是常說的事件分發、事件攔截、事件處理。

以「點擊中間那個 view」為例,down 事件下的傳遞鏈大致是:
1. Activity#dispatchTouchEvent() → ViewGroup#dispatchTouchEvent() → View#dispatchTouchEvent() 2. ViewGroup#onTouchEvent() → ViewGroup#onInterceptTouchEvent() → Activity#onTouchEvent() 3. ViewGroup 攔截成功後 → View#onTouchEvent() 回傳 true → 後續 move / up 交給該 View 的 onTouchEvent 處理 4. Activity#dispatchTouchEvent() 回傳 false,ViewGroup 攔截後 super.onTouchEvent 回傳 true → 事件由 ViewGroup 消費 5. View#dispatchTouchEvent() 最終交給 onInterceptTouchEvent(View 無此方法) → 直接走 onTouchEvent 回傳 true,事件被 View 消費 6. ViewGroup#dispatchTouchEvent() 若 onInterceptTouchEvent 回傳 false → 事件繼續下發給子 View;子 View 不消費時回到 ViewGroup#onTouchEvent 處理
想把這塊吃透,推薦一篇圖文並茂的《圖解 Android 事件分發機制》,搭配本文的實作一起看效果最好。
效果圖裡菜單展開與收起時都帶一點回彈,很討喜。實現方式是屬性動畫加上 setInterpolator,展開時用的是 mExpandAnim.setInterpolator(new OvershootInterpolator())。順手整理一下常用插值器的行為:
AccelerateDecelerateInterpolator:頭尾慢、中間快。
AccelerateInterpolator:起步慢,之後逐漸加速。
AnticipateInterpolator:先向後蓄力,再向前甩。
AnticipateOvershootInterpolator:先向後,再甩過頭,最後回到終值。
BounceInterpolator:結束時像球落地般彈跳。
CycleInterpolator:循環播放指定次數,速率沿正弦曲線變化。
DecelerateInterpolator:起步快而後減速。
LinearInterpolator:等速。
OvershootInterpolator:先甩過目標值,再退回原位。
如果不想要回彈手感,把插值器換成 LinearInterpolator(等速)或直接不設即可。
最後謝謝你耐心聽我嘮叨這麼久。有些地方若想不通透,建議直接把源碼下載下來,斷點跟一遍、編譯跑一次,很多疑惑會自然消散。使用方式真的很簡單:在你需要側滑刪除的 item 外層,用這個自定義 ViewGroup 包住就行。也要再次感謝原作者無私的分享。
如何讓手機一機兩用?四招讓你爽翻天
手機一機兩用的四種方法 通過應用雙開、分屏、懸浮窗與訪客模式,可以讓一部手機發揮更高效率。 微信雙開 可使用LBE平行空間等工具,將微信、QQ、微博或游戲添加為分身,登錄
微信怎麼玩?從支付、叫車到發紅包的入門指南
作為手機端的通訊應用,微信早已不只是一個聊天工具。如果你目前還停留在 QQ,還沒開始用微信,那大概會錯過不少便利——行動支付、叫車、發紅包,這些都在同一個 App 裡完成
Android Animation 動畫完全指南:四種基本動畫與逐幀動畫
先前在〈用 PopupWindow 實作仿騰訊新聞底部彈出菜單〉時用到了 Animation 來處理菜單的顯示與隱藏,效果比想像中簡單。本文把 Android 的 Ani
Eclipse 搭建 Android 開發環境完整流程(JDK / SDK / ADT / AVD 一次搞懂)
本文把 Eclipse 時代搭建 Android 開發環境的完整環節整理成一條主線:JDK → Eclipse → SDK → ADT → AVD → 第一支 Hello