tags:

views:

322

answers:

4

What Android Api is used to get the scrolling to the left or to the right on the start screen on Android?

+1  A: 

You are looking for GestureLibraries class. Read more about it here.

I haven't used this myself so I can't give you more info, but these might also be useful:

Android MotionEvent

Android - Basic Gesture Detection

Nikola Smiljanić
That means I have to basically create the gesture on my own and there isn't a fixed gesture for going left or right already in place?The gesture even works on the start screen of my phone with Android 1.5 while the class that you linked to is introduced in Android 1.6.
Christian
@Christian you don't have to build a complex gesture for a simple thing like a fling. There are built in convienience classes for common casses like that.
CodeFusionMobile
+1  A: 

It's all in the source repository for you to browse:

http://android.git.kernel.org/?p=platform/packages/apps/Launcher.git;a=blob;f=src/com/android/launcher/Workspace.java

In the later versions it's using ViewGroup.onInterceptTouchEvent and View.onTouchEvent to catch the drag, and doing the calculation manually based on the amount of movement from when the touch started.

The actual movement is just done via View.scrollBy.

ydant
+5  A: 

The simplest way is by detecting a "Fling" gesture. The android API has a built in detector for basic gestures like flinging, scrolling, long press, double tap, pinch zoom, etc.

The documentation is available at http://developer.android.com/reference/android/view/GestureDetector.html.

What you do is create an instance of GestureDetector, override the onTouchEvent method of the view you are interested in detecting gestures for, and pass the MotionEvent to the GestureDetector.

You also have to supply a OnGestureListener implementation (easiest to extend SimpleOnGestureListener) to the GestureDetector and that will handle all of your gesture events.

Example:

class MyView extends View
{
    GestureDetector mGestureDetect;

    public MyView(Context context)
    {
        super(context);
        mGestureDetect = new GestureDetector(new SimpleOnGestureListener()
        {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
        {
        //check if the fling was in the direction you were interested in
        if(e1.getX() - e2.getX() > 0)
        {
        //Do something here
        }
        //fast enough?
        if(velocityX > 50)
        {
        //etc etc
        }

        return true;
        }
        }
    }

    public boolean onTouchEvent(MotionEvent ev)
    {
        mGestureDetector.onTocuhEvent(ev);
        return true;
    }
}
CodeFusionMobile