Getting the current position of the ViewPager

I know that with the Gallery widget I was able to use getSelectedItemPosition (); to get the current position, however, it seems the ViewPager does not have this.

I know that I can configure the listener and get the position when switching the page. But I need a current position.

+78
android android-viewpager
Nov 24 '11 at 14:38
source share
4 answers

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; } } 
+89
Nov 24 2018-11-11T14:
source share

You can use:

 mViewPager.getCurrentItem() 
+181
Apr 05 '13 at 10:15
source share

Update 2019

Now you can set addOnPageChangeListener to the View Pager to Watch for changes in page position.

Since you wanted to tune the listener and get the position when the page switches

 mViewPager.addOnPageChangeListener(object : OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) {} override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {} override fun onPageSelected(position: Int) { pagePosition.setText("" + position + "/" + galleryAdapter!!.count) } }) 
0
Jun 26 '19 at
source share

I tell you that this is a hack, so there is no reason to lower your voice for this reason. This means that it will be useful for you or not. In any case, the description below will provide some information and will be useful to the community. In addition, this solution is suitable for older APIs that do not have ViewPager.getCurrentItem() .




First, some information. If you iterate over all the children of the ViewPager with ViewPager.getChildAt(x); and print with toString() (or getLeft() ) of each child view (page), and then do it every time you change the page, you will notice that the child elements will not be in the logical order in which they appear when start returning to the pages (going to the beginning). Apparently, it will remove the unnecessary child from the array, and then add the newest child to the array. So, for example, let's say you look at page 2 and then change to page 3, your list of children will be in the following order: page 2, page 3, page 4 meaning that ViewPager.getChildAt(1); will return the current page. But if you then return to page 2 (from page 3), your list of children will be in the following order: page 2, page 3, page 1 which means ViewPager.getChildAt(1); does not return the current page. I have not yet been able to find simple logic to weed out the current page using this information. Because the order of the pages in the array behind getChildAt is in random order depending on how the user browsed the pages.

In doing so, I developed a workaround for hacking. I have no idea if this feature will work in all environments, but it works for my current project. I suspect that if not for you, then this is a problem of a different API level. But in fact, I do not suspect any problems for other environments.

Now for the meat. I noticed that the result of ViewPager.getChildAt(x).getLeft() will have some type of horizontal pixel coordinate relative to the parent. So, I used this information to filter out which view is current.

 private int getCurrentPageIndex(ViewPager vp){ int first,second,id1,id2,left; id1 = first = second = 99999999; View v; for ( int i = 0, k = vp.getChildCount() ; i < k ; ++i ) { left = vp.getChildAt(i).getLeft(); if ( left < second ) { if ( left < first ) { second = first; id2 = id1; first = left; id1 = i; } else { second = left; id2 = i; } } } return id2; } 

This function is probably a dubious hack because it relies on the value of getLeft() to figure it all out. But I take the left coordinate of each child. Then I compare this with other values ​​and save the first and second pages, returning the second function (current page) from the function. It seems to work beautifully.

Why (you might ask) I just didn’t use onClickListenter or any onClickListenter solution? Well, I was damn sure there was a straightforward way to do this without having to include listeners, other classes, non-final focus, and other fanatics. Unfortunately, this solution is not entirely straightforward. But it saves from bloating, other classes and students. If I can find a more direct path, I rewrite this function. Or maybe it will give understanding for someone else to have insight.

-one
Mar 01 '13 at 21:05
source share



All Articles