【问题标题】:How can I limit fling in Android gallery to just one item per fling?如何将 Android 图库中的投掷限制为每次投掷仅一项?
【发布时间】:2010-11-30 08:34:01
【问题描述】:

我有一个包含几个全屏图像的画廊。我想将投掷手势限制为一次仅推进一张图像(如 HTC Gallery 应用程序)。实现这一目标的正确/最简单的方法是什么?

【问题讨论】:

    标签: android gallery


    【解决方案1】:

    只需重写 Gallery Widget 的 onFling() 方法,不要调用超类 onFling() 方法。

    这将使画廊在每次滑动时前进一个项目。

    【讨论】:

      【解决方案2】:

      我有同样的要求,但我发现如果我只返回 false,它每次只会滑动一个项目。

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

      【讨论】:

        【解决方案3】:

        回答问题的代码示例:

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

        【讨论】:

          【解决方案4】:

          我有一个解决方案,虽然它不能保证最多一次前进,但它非常简单(并且很可能会在代码中手动执行):只需降低 onFling 参数中的 x 速度。也就是说,覆盖 onFling 使其看起来像这样:

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

          最好的,

          迈克尔

          【讨论】:

            【解决方案5】:

            您好,遇到了同样的问题,我使用以下逻辑解决了问题。

            1-> 创建一个类应该扩展 Gallery
            2-> 并覆盖 onFling 方法。

            见下面的代码:

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

            在 xml 中使用这个类作为图库:


            <com.sra.GallView
                            android:id="@+id/Gallery01"
                            android:layout_width="fill_parent"
                            android:layout_height="250dip" >
                        </com.sra.GallView>
            

            【讨论】:

              【解决方案6】:

              我找不到任何限制滚动的方法,但我成功地解决了实现/适应此代码的问题: http://permalink.gmane.org/gmane.comp.handhelds.android.devel/101327

              它实现了一个带有“fling”的画廊

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-04-27
                • 2013-08-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多