开发者

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 开发者_JAVA百科way to achieve this?


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.


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


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

}


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


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>


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜