Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> ListView + PopupWindow實現滑動刪除,popupwindowlistview

ListView + PopupWindow實現滑動刪除,popupwindowlistview

編輯:關於android開發

ListView + PopupWindow實現滑動刪除,popupwindowlistview


 

原文:ListView滑動刪除 ,仿騰訊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); // 如果需要通過點擊PopupWindow之外的地方使其消失,則需要setFocusable(true). mPopupWindow.setFocusable(true); // Android 6.0以前的版本需要setBackgroundDrawable(), // 才能實現通過點擊PopupWindow之外的地方使其消失的功能。 mPopupWindow.setBackgroundDrawable(new ColorDrawable(0)); // 先調用下measure,否則拿不到寬和高 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; Log.d(TAG, "mTouchSlop = " + mTouchSlop + ", dx = " + dx + ", dy = " + dy); if(mXDown > x && Math.abs(dx) > mTouchSlop && Math.abs(dy) < mTouchSlop){ Log.d(TAG, "isSliding"); isSliding = true; int[] location = new int[2]; mCurrentView.getLocationOnScreen(location); mPopupWindow.setAnimationStyle(R.style.popwindow_delete_btn_anim_style); mPopupWindow.update(); Log.d(TAG, "Height: " + mCurrentView.getHeight() + "," + mPopupWindow.getHeight()); 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事件, // 而AbsListView的onTouchEvent調用了onTouchUp方法,在onTouchUp方法中有可能執行 // performClick.run() --> performItemClick() --> super.performItemClick // --> mOnItemClickListener.onItemClick,這樣最終觸發Item的點擊。 // 因此此處依舊保持isSliding為true的狀態,而在ACTION_DOWN事件中恢復isSliding為false, // 畢竟每個事件都以ACTION_DOWN開始。 //isSliding = false; } if(isSliding){ return true; } return super.onTouchEvent(ev); } public void setDelButtonClickListener(DelButtonClickListener listener){ mListener = listener; } interface DelButtonClickListener{ public void clickHappend(int position); } } MyListView.java

 

通過這個例子學習到:

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()中,對MotionEvent.ACTION_UP直接return true消費掉事件,而不要調用super.onTouchEvent。這樣就避免了刪除按鈕與Item點擊事件的沖突。

 

2、PopupWindow--通過點擊PopupWindow之外的地方使其消失

a、需要調用setFocusable()方法(PopupWindow中showAtLocation() --> createPopupLayoutParams() --> computeFlags() --> 設置FLAG_NOT_FOCUSABLE);

b、Android 6.0以前的版本需要setBackgroundDrawable()(具體原因見:PopupWindow的使用)。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved