18

I have a gallery with several full screen images. I want to limit the fling gesture to only advance one image at a time (like the HTC Gallery app). What's the right/easiest way to achieve this?

Gunnar Lium
  • 5,683
  • 9
  • 33
  • 33
  • Here is another possible answer! http://stackoverflow.com/questions/2373617/how-to-stop-scrolling-in-a-gallery-widget?lq=1 – Lunf Mar 26 '13 at 15:38

6 Answers6

27

Simply override the Gallery Widget's onFling() method and don't call the superclass onFling() method.

This will make the gallery advance one item per swipe.

olen_garn
  • 892
  • 7
  • 16
20

I had the same requirement and I just discovered that it will slide just one item per fling if I'll just return false.

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                       float velocityY) {        
    return false;
}
vmihalca
  • 201
  • 2
  • 2
12

code example to answer the question:

public class SlowGallery extends Gallery
{


    public SlowGallery(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public SlowGallery(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public SlowGallery(Context context)
    {
        super(context);
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {

        //limit the max speed in either direction
        if (velocityX > 1200.0f)
        {
            velocityX = 1200.0f;
        }
        else if(velocityX < -1200.0f)
        {
            velocityX = -1200.0f;
        }

        return super.onFling(e1, e2, velocityX, velocityY);
    }

}
Someone Somewhere
  • 22,369
  • 11
  • 111
  • 155
7

I have a solution, which, although it does not guarantee at most one advance, is extremely simple (and likely does what you would do manually in code): simply decrease the x-velocity in the onFling parameter. That is, override the onFling to simply look like this:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    return super.onFling(e1, e2, velocityX / 4, velocityY);
}

Best,

Michael

Michael F
  • 333
  • 3
  • 7
3

Hi faced same problem , i solved issue using below logic .

1-> Create One class that class Should extends Gallery
2-> and Override onFling method .

see below code :

package com.sra;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

public class GallView  extends Gallery{
public GallView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

public GallView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }


    public GallView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {        
        return false;
    }

}

use this class in xml as a gallery :


<com.sra.GallView
                android:id="@+id/Gallery01"
                android:layout_width="fill_parent"
                android:layout_height="250dip" >
            </com.sra.GallView>
Hagai L
  • 1,473
  • 1
  • 15
  • 39
sravan
  • 4,959
  • 1
  • 28
  • 33
0

I could not find any way to limit scrolling, but I solved the issue implementing/adapting with some success this code: http://permalink.gmane.org/gmane.comp.handhelds.android.devel/101327

it implements a gallery with "fling"

FrancescoR
  • 535
  • 1
  • 4
  • 12