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?
getChildAt(1); will return the current page. But, if you then change back to page 2 (from page 3) your list of children will be in this order page 2, page 3, page 1 which means that ViewPager. getChildAt(1); does not return the current page.
Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen. Today we're implementing a ViewPager by using Views and PagerAdapter. Though we can implement the same using Fragments too, but we'll discuss that in a later tutorial.
The ViewPager and pager adapter just deal with data in memory. So when data in memory is updated, we just need to call the adapter's notifyDataSetChanged() . Since the fragment is already created, the adapter's onItemPosition() will be called before notifyDataSetChanged() returns.
getCount() – Return the number of views available., i.e., number of pages to be displayed/created in the ViewPager.
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; } }
There is a method object_of_ViewPager.getCurrentItem()
which returns the position of currently Viewed page of view pager
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With