sam_k sam_k - 7 days ago 6
Android Question

How do you get the current page number of a ViewPager for Android?

I want to know the current page position of the

ViewPager
. I create an adapter and set this adapter to the
ViewPager
. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.

I used
viewPager.setCurrentItem(1);
for setting the first item.

Is there a similar method for getting the current page?

Answer

in the latest packages you can also use

vp.getCurrentItem()

or

vp is the viewPager ,

pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);

you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.

private int currentPage;

    private static class PageListener extends SimpleOnPageChangeListener{
            public void onPageSelected(int position) {
                Log.i(TAG, "page selected " + position);
                   currentPage = position;
        }
    }
Comments