图库一次滚动一张图像

发布于 2024-10-11 22:56:27 字数 797 浏览 1 评论 0原文

如何使图库控件一次滚动一张图像?另外,制作这些图像的连续循环的好方法是什么?我尝试重写 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();
            }

how to make a gallery control to scroll one image at a time? Also what is a good way of making a continuous loop of those images? I tried overriding onFling, does not work at all.

This moves image certain distance but does not really implement "true paging".

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-10-18 22:56:27

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

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

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

I created new control, called it CustomGallery and extended it from Gallery. In Custom Gallery I placed the following:

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

In my activity I am using CustomGallery instead of Gallery. This works. One thing, we moved from 2.2 to 2.3 (gingerbread). It didn't work for me before when I tried to override onFling. So I am suspecting this also have something to do with the version of OS.

盛夏已如深秋| 2024-10-18 22:56:27

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

Aniket Awati's solution worked best for me. However I would suggest an improvement to avoid two items beings scrolled in certain cases.

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;
}
淑女气质 2024-10-18 22:56:27

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

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

This works all the time. on all versions without fail for me.

    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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文