【问题标题】:Gallery scroll one image at a time图库一次滚动一张图片
【发布时间】:2011-01-14 02:12:44
【问题描述】:

如何使画廊控件一次滚动一张图片?还有什么是使这些图像连续循环的好方法?我尝试覆盖 onFling,根本不起作用。

这会将图像移动一定距离,但并没有真正实现“真正的分页”。

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

          //  return super.onFling(e1, e2, velocityX, velocityY);
            int kEvent;
              if(isScrollingLeft(e1, e2)){ //Check if scrolling left
                kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
              }
              else{ //Otherwise scrolling right
                kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
              }
              onKeyDown(kEvent, null);
              return true;  
        }
        private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
              return e2.getX() > e1.getX();
            }

【问题讨论】:

    标签: android gallery


    【解决方案1】:

    我创建了新控件,将其命名为 CustomGallery,并从 Gallery 扩展了它。在自定义图库中,我放置了以下内容:

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

    在我的活动中,我使用的是 CustomGallery 而不是 Gallery。这行得通。一件事,我们从 2.2 迁移到 2.3(姜饼)。当我尝试覆盖 onFling 之前,它对我不起作用。所以我怀疑这也与操作系统的版本有关。

    【讨论】:

    • 您不认为如果您获得 333 次浏览量,那么 Google 就应该开始在这里开发更直接的解决方案了吗? :)
    【解决方案2】:

    Aniket Awati 的解决方案最适合我。不过,我建议进行改进以避免在某些情况下滚动两个项目。

    int mSelection = 0;
    
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        boolean leftScroll = isScrollingLeft(e1, e2);
        boolean rightScroll = isScrollingRight(e1, e2);
    
        if (rightScroll) {
            if (mSelection != 0)             
                setSelection(--mSelection, true);
        } else if (leftScroll) {
    
            if (mSelection != getCount() - 1)
                setSelection(++mSelection, true);
        }
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      这一直有效。对我来说,所有版本都不会失败。

          private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
          return e2.getX() < e1.getX();
      }
      
      private boolean isScrollingRight(MotionEvent e1, MotionEvent e2) {
          return e2.getX() > e1.getX();
      }
      
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
              float velocityY) {
          boolean leftScroll = isScrollingLeft(e1, e2);
          boolean rightScroll = isScrollingRight(e1, e2);
      
          if (rightScroll) {
              if (getSelectedItemPosition() != 0)             
                  setSelection(getSelectedItemPosition() - 1, true);
          } else if (leftScroll) {
      
              if (getSelectedItemPosition() != getCount() - 1)
                  setSelection(getSelectedItemPosition() + 1, true);
          }
          return true;
      }
      

      【讨论】:

      • 你能举个例子吗,很紧急
      猜你喜欢
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多