In my Android application, I am using a viewpager for image swiping. My requirement is, if a user swipes out of the first and last page, the activity should finish.
I ha
I changed M.A.Murali's code:
public class CheckBoundsPager extends ViewPager {
public CheckBoundsPager(Context context) {
super(context);
}
public CheckBoundsPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
OnSwipeOutListener mListener;
public void setOnSwipeOutListener(OnSwipeOutListener listener) {
mListener = listener;
}
private float mStartDragX;
private float mStartDragY;
private float MIN_MOVE=50; //minimal movement in px - change it to you value
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
//here we set x and y on user start drag
mStartDragX = x;
mStartDragY = y;
}
break;
case MotionEvent.ACTION_MOVE: {
//most important to check if x drag is more than y
float xDiff=Math.abs(mStartDragX-x);
float yDiff=Math.abs(mStartDragY-y);
//added condition if swipe y is less than x
if (mStartDragX < x-MIN_MOVE && xDiff>yDiff && getCurrentItem() == 0) {
mListener.onSwipeOutAtStart();
} else if (mStartDragX > x+MIN_MOVE
&& xDiff>yDiff && getCurrentItem() == getAdapter().getCount() - 1) {
mListener.onSwipeOutAtEnd();
}
}
break;
}
return super.onInterceptTouchEvent(ev);
}
public interface OnSwipeOutListener {
public void onSwipeOutAtStart();
public void onSwipeOutAtEnd();
}
}
This solutions checks if we drag left or right and our drag's movement is in the x-axis. Remember to change MIN_MOVE
.
In my case, onInterceptTouchEvent does not work. I change the method with onTouchEvent and it is working. The original solution is from tjlian616
@Override
public boolean onTouchEvent(MotionEvent ev){
if(getCurrentItem()==getAdapter().getCount()-1){
final int action = ev.getAction();
float x = ev.getX();
switch(action & MotionEventCompat.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
mStartDragX = x;
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if (x<mStartDragX){
mListener.onSwipeOutAtEnd();
}else{
mStartDragX = 0;
}
break;
}
}else{
mStartDragX=0;
}
return super.onTouchEvent(ev);
}
*You can achive this by impementing OnPageChangeListener
http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html
myPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected( int i) {
if(i == 0 || i == list.size()- 1){
finish();
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});*
I have also used the code from this SO answer. Make sure you make the necessary changes to your activity class. Here's what I did...
I changed my activity so it implements CustomViewPager.OnSwipeOutListener
.
In my activity, after my mPager = (CustomViewPager) findViewById(R.id.pager);
line, I call
mPager.setOnSwipeOutListener(this);
All you need to do then is override the onSwipeOutAtStart()
and onSwipeOutAtEnd()
methods in your activity.