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.
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.
This function is deprecated.
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 .
You can use:
mViewPager.getCurrentItem()
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; } }
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