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.