Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit fling in Android gallery to just one item per fling?

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?

like image 840
Gunnar Lium Avatar asked Nov 30 '10 08:11

Gunnar Lium


5 Answers

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.

like image 136
olen_garn Avatar answered Nov 07 '22 09:11

olen_garn


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;
}
like image 30
vmihalca Avatar answered Nov 07 '22 11:11

vmihalca


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);
    }

}
like image 12
Someone Somewhere Avatar answered Nov 07 '22 09:11

Someone Somewhere


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

like image 7
Michael F Avatar answered Nov 07 '22 11:11

Michael F


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>
like image 3
sravan Avatar answered Nov 07 '22 10:11

sravan