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

前端 未结 6 1473
半阙折子戏
半阙折子戏 2020-11-30 03:43

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

相关标签:
6条回答
  • 2020-11-30 03:54

    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.

    0 讨论(0)
  • 2020-11-30 04:00

    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 讨论(0)
  • 2020-11-30 04:06

    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

    0 讨论(0)
  • 2020-11-30 04:07

    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);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-30 04:07

    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>
    
    0 讨论(0)
  • 2020-11-30 04:15

    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;
    }
    
    0 讨论(0)
提交回复
热议问题