Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 742
sam_k Avatar asked Nov 14 '11 05:11

sam_k


People also ask

How do I get ViewPager current position?

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.

How do I use ViewPager on Android?

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.

How do I update my Android ViewPager adapter?

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.

Which method returns the number of available views in ViewPager?

getCount() – Return the number of views available., i.e., number of pages to be displayed/created in the ViewPager.


2 Answers

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;         }     } 
like image 188
Yashwanth Kumar Avatar answered Sep 28 '22 06:09

Yashwanth Kumar


There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager

like image 39
Krishna Shrestha Avatar answered Sep 28 '22 05:09

Krishna Shrestha