Android程式開發之ListView 與PopupWindow實現從左向右滑動刪除功能_Android

來源:互聯網
上載者:User

文章實現的功能是:在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();}@Overridepublic 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() {@Overridepublic 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);}}

通過這個例子學習到:

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()方法;

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

以上所述是小編給大家介紹的Android程式開發之ListView 與PopupWindow實現滑動刪除功能,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.