編輯:關於 Android 程式設計
本文參考〈Android 仿騰訊 QQ 實現滑動刪除〉一文進行學習。該文實作的功能是:在 ListView 的 Item 上從右向左滑動時出現刪除按鈕,點擊刪除按鈕即可刪除該 Item,效果如下:

讀完原文後,我認為沒有必要同時重寫 dispatchTouchEvent() 與 onTouchEvent() 兩個方法,只要重寫 onTouchEvent() 即可。於是對程式碼做了一些調整:
public class MyListView extends ListView { private static final String TAG = "MyListView"; private int mTouchSlop; private int mXDown; private int mYDown; private int mCurrentPosition; private View mCurrentView; private PopupWindow mPopupWindow; private LayoutInflater mInflater; private boolean isSliding = false; private DelButtonClickListener mListener; private Button mDelBtn; private int mPopupWindowHeight; private int mPopupWindowWidth; public MyListView(Context context, AttributeSet attrs) { super(context, attrs); mInflater = LayoutInflater.from(context); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); View view = mInflater.inflate(R.layout.delete_btn, null); mDelBtn = (Button) view.findViewById(R.id.id_item_btn); mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); mPopupWindow.setFocusable(true); // Android 6.0以前的版本需要setBackgroundDrawable() mPopupWindow.setBackgroundDrawable(new ColorDrawable(0)); mPopupWindow.getContentView().measure(0, 0); mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight(); mPopupWindowWidth = mPopupWindow.getContentView().getMeasuredWidth(); } @Override public boolean onTouchEvent(MotionEvent ev) { int action = ev.getAction(); int x = (int) ev.getX(); int y = (int) ev.getY(); switch (action){ case MotionEvent.ACTION_DOWN: isSliding = false; mXDown = x; mYDown = y; mCurrentPosition = pointToPosition(mXDown, mYDown); View view = getChildAt(mCurrentPosition - getFirstVisiblePosition()); mCurrentView = view; break; case MotionEvent.ACTION_MOVE: int dx = x - mXDown; int dy = y - mYDown; if(mXDown > x && Math.abs(dx) > mTouchSlop && Math.abs(dy) < mTouchSlop){ isSliding = true; int[] location = new int[2]; mCurrentView.getLocationOnScreen(location); mPopupWindow.setAnimationStyle(R.style.popwindow_delete_btn_anim_style); mPopupWindow.update(); mPopupWindow.showAtLocation(mCurrentView, Gravity.NO_GRAVITY, location[0] + mCurrentView.getWidth(), location[1] + mCurrentView.getHeight() / 2 - mPopupWindowHeight / 2); mDelBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mListener.clickHappend(mCurrentPosition); mPopupWindow.dismiss(); } }); } case MotionEvent.ACTION_UP: // isSliding 若在此恢復為false,後續會執行super.onTouchEvent, // 進而觸發Item的點擊。因此保持isSliding為true,改在ACTION_DOWN中恢復false。 //isSliding = false; } if(isSliding){ return true; } return super.onTouchEvent(ev); } public void setDelButtonClickListener(DelButtonClickListener listener){ mListener = listener; } interface DelButtonClickListener{ public void clickHappend(int position); } }
透過這個範例,我學習到兩點:
1. ListView 的 Item 點擊事件觸發流程:
自訂 ListView 的 onTouchEvent() → 呼叫 super.onTouchEvent() → AbsListView.onTouchEvent() →(MotionEvent.ACTION_UP)→ AbsListView.onTouchUp() →(有可能)performClick.run() → AbsListView.PerformClick.run() → performItemClick() → AbsListView.performItemClick() →(有可能)super.performItemClick() → AdapterView.performItemClick() → mOnItemClickListener.onItemClick() → OnItemClickListener.onItemClick()。
也就是說,Item 的點擊事件是在 MotionEvent.ACTION_UP 時完成的。因此在自訂 ListView 的 onTouchEvent() 中,對 ACTION_UP 直接 return true 消費掉事件、不呼叫 super.onTouchEvent,就能避免刪除按鈕與 Item 點擊事件的衝突。
2. PopupWindow — 透過點擊外部區域使其消失:
a. 需要呼叫 setFocusable() 方法(其內部在 showAtLocation() → createPopupLayoutParams() → computeFlags() 中設定 FLAG_NOT_FOCUSABLE);
b. Android 6.0 以前的版本需要呼叫 setBackgroundDrawable()。
以上就是本文的全部內容,希望對大家的學習有所幫助。
Android 自定義倒計時控件 TimeTextView 實作解析
自定義倒數計時 TextView 的思路 在促銷、限時活動或頁籤倒數等場景中,常需要一個能持續更新剩餘時間的文字元件。這裡的做法是自訂 TimeTextView,讓它繼
踩坑筆記:Android 4.4 以上相冊選圖後無法裁剪的 URI 難題與解法
做相冊選圖加裁剪時,我卡了快半天才真正解決,這裡把過程與結論記錄下來。 問題現場 選好相冊照片、準備進入裁剪時出錯,華為手機直接提示「此圖片無法獲取」。搜尋後才理解,這
Android 圖片特效實作:素描、陽刻與印刻處理
本文以「妙趣剪紙」的圖片處理流程為例,說明如何在 Android 上將一般照片轉換成具有紙藝風格的素描、陽刻與印刻效果。整體思路並非依賴單一濾鏡,而是把灰階化、反色、高斯
Android 自定義多級樹形列表控件(支持任意層級)
在開發 App 的過程中,我們經常會遇到需要多級列表展示的需求,而 Android 原生 SDK 並沒有提供三級、四級甚至更多層級的列表控件。因此我們需要自己實現一個類似