Ionic Walrus
Ionic Walrus

Reputation: 1645

Android HorizontalScrollView scroll by page

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 appropriate way to do this, should I be using a FrameLayout instead ? How do I scroll to the next (or previous) image on scroll gesture ?

Upvotes: 2

Views: 6942

Answers (3)

Cheryl Simon
Cheryl Simon

Reputation: 46844

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.

Upvotes: 2

Sandeep Chayapathi
Sandeep Chayapathi

Reputation: 41

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

Upvotes: 1

Ionic Walrus
Ionic Walrus

Reputation: 1645

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".

Upvotes: 0

Related Questions