图库一次滚动一个图像

dro*_*ter 1 android gallery

如何使图库控件一次滚动一个图像?还有什么是制作这些图像的连续循环的好方法?我尝试重写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();
            }
Run Code Online (Sandbox Code Playgroud)

dro*_*ter 9

我创建了新的控件,称为CustomGallery,并从Gallery扩展.在Custom Gallery中,我放置了以下内容:

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

在我的活动中,我使用CustomGallery而不是Gallery.这有效.有一点,我们从2.2变为2.3(姜饼).在我尝试覆盖onFling之前,它对我不起作用.所以我怀疑这也与操作系统的版本有关.

  • 难道你不认为如果你得到333次关于谷歌开始制作一个更简单的解决方案的时间吗?:) (2认同)

小智 5

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;
}
Run Code Online (Sandbox Code Playgroud)