I want to get fling
gesture detection working in my Android application.
What I have is a GridLayout
that contains 9 ImageView
s. The source can be found here: Romain Guys's Grid Layout.
That file I take is from Romain Guy's Photostream application and has only been slightly adapted.
For the simple click situation I need only set the onClickListener
for each ImageView
I add to be the main activity
which implements View.OnClickListener
. It seems infinitely more complicated to implement something that recognizes a fling
. I presume this is because it may span views
?
If my activity implements
OnGestureListener
I don't know how to set that as the gesture listener for theGrid
or theImage
views that I add.public class SelectFilterActivity extends Activity implements View.OnClickListener, OnGestureListener { ...
If my activity implements
OnTouchListener
then I have noonFling
method tooverride
(it has two events as parameters allowing me to determine if the fling was noteworthy).public class SelectFilterActivity extends Activity implements View.OnClickListener, OnTouchListener { ...
If I make a custom
View
, likeGestureImageView
that extendsImageView
I don't know how to tell the activity that afling
has occurred from the view. In any case, I tried this and the methods weren't called when I touched the screen.
I really just need a concrete example of this working across views. What, when and how should I attach this listener
? I need to be able to detect single clicks also.
// Gesture detection
mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int dx = (int) (e2.getX() - e1.getX());
// don't accept the fling if it's too short
// as it may conflict with a button push
if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.absvelocityY)) {
if (velocityX > 0) {
moveRight();
} else {
moveLeft();
}
return true;
} else {
return false;
}
}
});
Is it possible to lay a transparent view over the top of my screen to capture flings?
If I choose not to inflate
my child image views from XML can I pass the GestureDetector
as a constructor parameter to a new subclass of ImageView
that I create?
This is the very simple activity that I'm trying to get the fling
detection to work for: SelectFilterActivity (Adapted from photostream).
I've been looking at these sources:
Nothing has worked for me so far and I was hoping for some pointers.
There's some proposition over the web (and this page) to use ViewConfiguration.getScaledTouchSlop() to have a device-scaled value for
SWIPE_MIN_DISTANCE
.getScaledTouchSlop()
is intended for the "scrolling threshold" distance, not swipe. The scrolling threshold distance has to be smaller than a "swing between page" threshold distance. For example, this function returns 12 pixels on my Samsung GS2, and the examples quoted in this page are around 100 pixels.With API Level 8 (Android 2.2, Froyo), you've got
getScaledPagingTouchSlop()
, intended for page swipe. On my device, it returns 24 (pixels). So if you're on API Level < 8, I think "2 *getScaledTouchSlop()
" should be the "standard" swipe threshold. But users of my application with small screens told me that it was too few... As on my application, you can scroll vertically, and change page horizontally. With the proposed value, they sometimes change page instead of scrolling.You can use the droidQuery library to handle flings, clicks, long clicks, and custom events. The implementation is built on my previous answer below, but droidQuery provides a slick, simple syntax:
Original Answer
This answer uses a combination of components from the other answers here. It consists of the
SwipeDetector
class, which has an inner interface for listening for events. I also provide aRelativeLayout
to show how to override aView
'sonTouch
method to allow both swipe events and other detected events (such as clicks or long clicks).SwipeDetector
Swipe Interceptor View
One of the answers above mentions handling different pixel density but suggests computing the swipe parameters by hand. It is worth noting that you can actually obtain scaled, reasonable values from the system using
ViewConfiguration
class:I noticed that using these values causes the "feel" of fling to be more consistent between the application and rest of system.
I nedded a more generic Class , I took Tomas's class and added an Interface that send events to your Activity or Fragment. it will register the listener on the constructor so be sure you implement the interface or an ClassCastException will be thorwn. the interface returns one of the four final int defined in the class and will return the view on which it was activated upon.
I do it a little different, and wrote an extra detector class that implements the
View.onTouchListener
onCreate
is simply add it to the lowest layout like this:where id.lowestLayout is the id.xxx for the view lowest in the layout hierarchy and lowestLayout is declared as a RelativeLayout
And then there is the actual activity swipe detector class:
Works really good for me!
There's a built-in interface that you can use directly for all gestures:
Here is an explanation for a basic level user: There is 2 imports be careful in choosing that both are diferent