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

Android 以 ListView 與 PopupWindow 實作滑動刪除功能

編輯:關於 Android 程式設計

本文要實作的功能是:在 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); 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() 方法;
b. Android 6.0 以前的版本需要 setBackgroundDrawable()。

以上便是本文介紹的 ListView 與 PopupWindow 實作滑動刪除功能的完整做法,若有任何疑問歡迎留言討論。

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