package com.qudoulicai.www.qqlistview.QQlistview; import android.content.Context; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.ListView; public class QQlistview extends ListView { private int mScreenWidth; // 屏幕寬度 private int mDownX; // 按下點的x值 private int mDownY; // 按下點的y值 private int mDeleteBtnWidth;// 刪除按鈕的寬度 private boolean isDeleteShown; // 刪除按鈕是否正在顯示 private ViewGroup mPointChild; // 當前處理的item private LinearLayout.LayoutParams mLayoutParams; // 當前處理的item的LayoutParams public QQlistview(Context context, AttributeSet attrs) { this(context, attrs, 0); } public QQlistview(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // 獲取屏幕寬度 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: performActionDown(ev); break; case MotionEvent.ACTION_MOVE: return performActionMove(ev); case MotionEvent.ACTION_UP: performActionUp(); break; } return super.onTouchEvent(ev); } // 處理action_down事件 private void performActionDown(MotionEvent ev) { if(isDeleteShown) { turnToNormal(); } mDownX = (int) ev.getX(); mDownY = (int) ev.getY(); // 獲取當前點的item mPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY) - getFirstVisiblePosition()); // 獲取刪除按鈕的寬度 mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width; mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0) .getLayoutParams(); // 爲何要從新設置layout_width 等於屏幕寬度 // 由於match_parent時,無論你怎麼滑,都不會顯示刪除按鈕 // why? 由於match_parent時,ViewGroup就不去佈局剩下的view mLayoutParams.width = mScreenWidth; mPointChild.getChildAt(0).setLayoutParams(mLayoutParams); } // 處理action_move事件 private boolean performActionMove(MotionEvent ev) { int nowX = (int) ev.getX(); int nowY = (int) ev.getY(); if(Math.abs(nowX - mDownX) > Math.abs(nowY - mDownY)) { // 若是向左滑動 if(nowX < mDownX) { // 計算要偏移的距離 int scroll = (nowX - mDownX) / 2; // 若是大於了刪除按鈕的寬度, 則最大爲刪除按鈕的寬度 if(-scroll >= mDeleteBtnWidth) { scroll = -mDeleteBtnWidth; } // 從新設置leftMargin mLayoutParams.leftMargin = scroll+10; mPointChild.getChildAt(0).setLayoutParams(mLayoutParams); } return true; } return super.onTouchEvent(ev); } // 處理action_up事件 private void performActionUp() { // 偏移量大於button的一半,則顯示button // 不然恢復默認 if(-mLayoutParams.leftMargin >= mDeleteBtnWidth / 2) { mLayoutParams.leftMargin = -mDeleteBtnWidth; isDeleteShown = true; }else { turnToNormal(); } mPointChild.getChildAt(0).setLayoutParams(mLayoutParams); } /** * 變爲正常狀態 */ public void turnToNormal() { mLayoutParams.leftMargin = 0; mPointChild.getChildAt(0).setLayoutParams(mLayoutParams); isDeleteShown = false; } /** * 當前是否可點擊 * @return 是否可點擊 */ public boolean canClick() { return !isDeleteShown; } } //非原創原創地址太偏。。搬運過來