Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current position of a ViewPager

I know with the Gallery widget I was able to use getSelectedItemPosition(); to retrieve the current position, however it doesnt seem ViewPager has that.

I know I can setup a listener and retrieve the position when the page is switched. But I want the current view position.

like image 709
Adam Avatar asked Nov 24 '11 14:11

Adam


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.

Is ViewPager deprecated?

This function is deprecated.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .


2 Answers

You can use:

mViewPager.getCurrentItem() 
like image 162
Terry Avatar answered Oct 03 '22 19:10

Terry


Create a listener and set it on your viewpager:

/**  * Get the current view position from the ViewPager by  * extending SimpleOnPageChangeListener class and adding your method  */ public class DetailOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {      private int currentPage;      @Override     public void onPageSelected(int position) {         currentPage = position;     }      public final int getCurrentPage() {         return currentPage;     } } 
like image 30
androidu Avatar answered Oct 03 '22 17:10

androidu