From 40c8b8ef1203a3e8e5ffd97821a88db67263f4a1 Mon Sep 17 00:00:00 2001 From: baoyz Date: Fri, 5 Sep 2014 16:06:50 +0800 Subject: [PATCH] add OnSwipeListener --- .../swipemenulistview/SwipeMenuListView.java | 50 ++++++++++++------- sample/AndroidManifest.xml | 2 +- .../swipemenulistviewsample/MainActivity.java | 17 ++++++- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/library/src/com/baoyz/swipemenulistview/SwipeMenuListView.java b/library/src/com/baoyz/swipemenulistview/SwipeMenuListView.java index 13b1b52..6af690a 100644 --- a/library/src/com/baoyz/swipemenulistview/SwipeMenuListView.java +++ b/library/src/com/baoyz/swipemenulistview/SwipeMenuListView.java @@ -31,6 +31,7 @@ public class SwipeMenuListView extends ListView { private int mTouchState; private int mTouchPosition; private SwipeMenuLayout mTouchView; + private OnSwipeListener mOnSwipeListener; private SwipeMenuCreator mMenuCreator; private OnMenuItemClickListener mOnMenuItemClickListener; @@ -81,23 +82,23 @@ public void onItemClick(SwipeMenuView view, SwipeMenu menu, } }); } - - public void setCloseInterpolator(Interpolator interpolator){ + + public void setCloseInterpolator(Interpolator interpolator) { mCloseInterpolator = interpolator; } - - public void setOpenInterpolator(Interpolator interpolator){ + + public void setOpenInterpolator(Interpolator interpolator) { mOpenInterpolator = interpolator; } - - public Interpolator getOpenInterpolator(){ + + public Interpolator getOpenInterpolator() { return mOpenInterpolator; } - - public Interpolator getCloseInterpolator(){ + + public Interpolator getCloseInterpolator() { return mCloseInterpolator; } - + @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return super.onInterceptTouchEvent(ev); @@ -105,9 +106,9 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { @Override public boolean onTouchEvent(MotionEvent ev) { - if(ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null) + if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null) return super.onTouchEvent(ev); - int action = MotionEventCompat.getActionMasked(ev); + int action = MotionEventCompat.getActionMasked(ev); action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: @@ -117,7 +118,7 @@ public boolean onTouchEvent(MotionEvent ev) { mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); View view = getChildAt(mTouchPosition - getFirstVisiblePosition()); - + if (mTouchView != null && mTouchView.isOpen()) { mTouchView.smoothCloseMenu(); mTouchView = null; @@ -141,14 +142,14 @@ public boolean onTouchEvent(MotionEvent ev) { ev.setAction(MotionEvent.ACTION_CANCEL); super.onTouchEvent(ev); return true; - } else { - // Log.i("byz", "state = " + mTouchState + ", dx = " + dx - // + ", max = " + MAX_X); - if (mTouchState != TOUCH_STATE_X && Math.abs(dy) > MAX_Y) { + } else if (mTouchState == TOUCH_STATE_NONE) { + if (Math.abs(dy) > MAX_Y) { mTouchState = TOUCH_STATE_Y; - } - if (mTouchState != TOUCH_STATE_Y && dx > MAX_X) { + } else if (dx > MAX_X) { mTouchState = TOUCH_STATE_X; + if (mOnSwipeListener != null) { + mOnSwipeListener.onSwipeStart(mTouchPosition); + } } } break; @@ -157,6 +158,9 @@ public boolean onTouchEvent(MotionEvent ev) { if (mTouchView != null) { mTouchView.onSwipe(ev); } + if (mOnSwipeListener != null) { + mOnSwipeListener.onSwipeEnd(mTouchPosition); + } ev.setAction(MotionEvent.ACTION_CANCEL); super.onTouchEvent(ev); return true; @@ -180,7 +184,17 @@ public void setOnMenuItemClickListener( this.mOnMenuItemClickListener = onMenuItemClickListener; } + public void setOnSwipeListener(OnSwipeListener onSwipeListener) { + this.mOnSwipeListener = onSwipeListener; + } + public static interface OnMenuItemClickListener { void onMenuItemClick(int position, SwipeMenu menu, int index); } + + public static interface OnSwipeListener { + void onSwipeStart(int position); + + void onSwipeEnd(int position); + } } diff --git a/sample/AndroidManifest.xml b/sample/AndroidManifest.xml index 805bc2a..016251b 100644 --- a/sample/AndroidManifest.xml +++ b/sample/AndroidManifest.xml @@ -7,7 +7,7 @@ - +