Skip to content

Commit

Permalink
add OnSwipeListener
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyongzhang committed Sep 5, 2014
1 parent f67a8d1 commit 40c8b8e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
50 changes: 32 additions & 18 deletions library/src/com/baoyz/swipemenulistview/SwipeMenuListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,33 +82,33 @@ 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);
}

@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:
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion sample/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
Expand Down
17 changes: 15 additions & 2 deletions sample/src/com/baoyz/swipemenulistviewsample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
Expand All @@ -28,6 +26,7 @@
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;

public class MainActivity extends Activity {

Expand Down Expand Up @@ -103,6 +102,20 @@ public void onMenuItemClick(int position, SwipeMenu menu, int index) {
}
}
});

// set SwipeListener
listView.setOnSwipeListener(new OnSwipeListener() {

@Override
public void onSwipeStart(int position) {
// swipe start
}

@Override
public void onSwipeEnd(int position) {
// swipe end
}
});

// other setting
// listView.setCloseInterpolator(new BounceInterpolator());
Expand Down

0 comments on commit 40c8b8e

Please sign in to comment.