开发者

Android HorizontalScrollView scroll by page

开发者 https://www.devze.com 2022-12-21 01:22 出处:网络
I have implemented a slideshow in my Android app using . This works well except that I want to scroll to next image on a scroll gesture (now it just scrolls past few images before decelerating). I hav

I have implemented a slideshow in my Android app using . This works well except that I want to scroll to next image on a scroll gesture (now it just scrolls past few images before decelerating). I have couldn't find a appropriat开发者_开发技巧e way to do this, should I be using a FrameLayout instead ? How do I scroll to the next (or previous) image on scroll gesture ?


From your description it sounds like right now you have your slideshow implemented as a series of images in a scrollview, is that correct?

Rather than placing it in a scrollview and allowing that view to do the scrolling, you could display a single image and listen for a fling or scroll gesture on the image (see documentation). When you detect the gesture you could then manually change the image.

If you want it to animate the image coming onto the screen, you could use an animation.


This is what I ended up using

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub

    if (velocityX <= 0 ){
        // hack - send event to simulate right key press
        KeyEvent rightKey  = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT);
        this.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, rightKey);

        rightKey = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_RIGHT);
        this.onKeyUp(KeyEvent.KEYCODE_DPAD_RIGHT, rightKey);            

    }else{
        // hack - send event to simulate left key press
        KeyEvent leftKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT);
        this.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, leftKey);

        leftKey = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_LEFT);
        this.onKeyUp(KeyEvent.KEYCODE_DPAD_LEFT, leftKey);
    }


    // your callback activity if you have one:
    if( callbackActivity != null ){
        callbackActivity.didFlingGallery();
    }

    return true;
}


the moment you post a question you realize the answer! I went from using Gallery to HorizontalScrollView back to using Gallery. Just shows my n00bness. Gallery solved my problem nicely. I have a custom class that extends Gallery and overrides onFling(...). In this reset the velocityX to say -300 or 300 depending on the direction, this results in scrolling to the next "page".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号