1

I want to create a viewpager at the top of my layout to allow users to swipe through a gallery images. When the page switches I want to change the text below that reflects the specific images. Is there a method I that listens for this action?

Also, I would like this slider to automatically switch every 5 seconds. Is there a method that switches?

Adam
  • 8,849
  • 16
  • 67
  • 131

2 Answers2

4

In order to detect when a ViewPager has changed views you need to implment ViewPager.OnPageChangeListener and set it on the ViewPager:

viewPager.setOnPageChangeListener(new MyOnPageChangeListener);

which should have this:

// view pager item selected

public void onPageSelected(int i) {
    updateUI(i);
}

A full working example with an updating text view is on my site here: http://responsiveandroid.com/2012/01/26/android-viewpager-and-gallery-integration.html

To automatically page you need to use the Handler class

(untested)

handler.postDelayed(new Runnable() {
        public void run() {
            viewPager.setCurrentItem(viewPagerCurrentItem++);
        }
    },5000);
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
browep
  • 5,057
  • 5
  • 29
  • 37
0

Have a look at this and Gestures for swipe detection.

And you can use timer for automatic switching. Check this

Community
  • 1
  • 1
Permita
  • 5,503
  • 1
  • 16
  • 21