Created
August 27, 2012 18:38
-
-
Save christopherperry/3491216 to your computer and use it in GitHub Desktop.
Android LinearLayout that handles left swipes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.ViewConfiguration; | |
import android.widget.LinearLayout; | |
public class SwipeLinearLayout extends LinearLayout { | |
private GestureDetector gestureDetector; | |
private OnSwipeListener listener; | |
private int lastMotionX; | |
private int lastMotionY; | |
public interface OnSwipeListener { | |
void onSwipeLeft(); | |
} | |
public SwipeLinearLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
gestureDetector = new GestureDetector(getContext(), new LeftSwipeListener()); | |
} | |
public void setOnSwipeListener(OnSwipeListener listener) { | |
this.listener = listener; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent event) { | |
int action = event.getAction(); | |
if (action == MotionEvent.ACTION_DOWN) { | |
lastMotionX = (int) event.getX(); | |
lastMotionY = (int) event.getY(); | |
} | |
final int scaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); | |
final int x = (int) event.getX(); | |
final int y = (int) event.getY(); | |
final int diffX = Math.abs(x - lastMotionX); | |
final int diffY = Math.abs(y - lastMotionY); | |
boolean isSwipingSideways = diffX > scaledTouchSlop && diffX > diffY; | |
// Start sending all events to our onTouchEvent from this point | |
if (action == MotionEvent.ACTION_MOVE && isSwipingSideways) { | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
return gestureDetector.onTouchEvent(event); | |
} | |
private class LeftSwipeListener extends GestureDetector.SimpleOnGestureListener { | |
private static final int SWIPE_MIN_DISTANCE = 120; | |
private static final int SWIPE_THRESHOLD_VELOCITY = 1200; // Toy with this value to adjust how hard you have to swipe | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
// right to left swipe | |
if(lastMotionX - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { | |
if (listener != null) { | |
listener.onSwipeLeft(); | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
} |
Hi, what should be called from onSwipeLeft() ? Is it possible to implement this functionality to android.support.v4.view.ViewPager class? ty.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I'm new with android and java.
How do I "connect" this class to the horizontal linear layout in my app?
rg,
Eric