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.

-------------Problems Reply------------

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;
}
}

You can use:

mViewPager.getCurrentItem()

Well, to anyone who was actually paying attention... my previous solution didnt actually work. So I deleted it and came up with a hack that seems to work great! LOL!

First, a little information. If you iterate thru all children of a ViewPager with ViewPager.getChildAt(x); and print out with toString() (or getLeft()) each child View (a page) and then do this everytime you change pages, you will notice that the children will not be in the logical order that they are displayed in when you start going back pages (paging back to the beginning). Apparently, it will remove the unneeded child from the array then append the newest child to the array. So, for example, lets say you are looking at page 2 then changed to page 3, your list of children will be in this order page 2, page 3, page 4 meaning that ViewPager.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. 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 an arbitrary order based off of how the user has been paging around.

That being said, I developed a hack work-around. I have no clue if this function will work in all in environments, but it works for my current project. I would suspect if doesn't for you, then its an issue of different API level. But I dont actually suspect any issues for other environments.

Now, onto the meat. What I noticed was 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 weed out which view is the current one.

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 questionable hack because it relies on the value of getLeft() to figure it all out. But, I grab the left coordinate of each child. I then compare this to the other values and store the first and second pages, returning the second page (current page) out of the function. It seems to work beautifully.

Why (might you ask) didnt I just use onClickListenter or whatever solution? Well, I was darned determined that there was a straight forward way to do this without having to include listeners, other classes, inconclusive focus, and other bloat. Unfortunately, this solution is not exactly straight forward. But, it does do away with the bloat, other classes and listeners. If I can figure out a more straight forward way, I will be rewriting this function. Or maybe, this will provide insight for someone else to have an epiphany.

Category:android Views:0 Time:2011-11-24

Related post

  • Get focused View from ViewPager 2011-07-24

    i use the ViewPager for switching views with left/right swipe. The ViewPager needs an Adapter, so I've built this one: public class ListViewPagerAdapter extends PagerAdapter { protected static final String TAG = "ListViewPagerAdapter"; protected stat

  • Dynamically load next fragment with ViewPager 2011-08-01

    I'm creating a demo app with the new android compatibility library. I want to show two listfragments with ViewPager. The first shows the root categories and the second shows the subcategories. After somebody clicks on root category the viewpager move

  • ViewPager inside ViewPager 2011-08-17

    I would like to create a ViewPager (with three items) where each of its view is another ViewPager (with two items). User then swipe items like this: ViewPager1[0] ViewPager2[0] ViewPager1[0] ViewPager2[1] ViewPager1[1] ViewPager2[0] ViewPager1[1] Vie

  • ViewPager PagerAdapter not updating the View 2011-08-31

    I'm using the ViewPager from the compatibility library. I have succussfully got it displaying several views which I can page through. However, I'm having a hard time figuring out how to update the ViewPager with a new set of Views. I've tried all sor

  • InstantiateItem in PagerAdapter and AddView in ViewPager confusion 2011-09-01

    So far I've been hacking together examples to get a feel for the APIs the Anfroid SDK as a whole. However I've hit an impasse. I'm trying to Inflate a LinearLayout with 2 TextViews from an XML file. These will be then Views that are paged. I simply c

  • Android ViewPager + Webview, ClassCastException! Need webview in a viewpager 2011-09-08

    I am trying to create a horizontal "listview" of webviews. As suggested by a previous answer to a question, I am using the ViewPager library from the compatibility pack. Unfortunately when I go to add my webview to the view collection, using ((ViewPa

  • How to force ViewPager to re-instantiate its items 2011-09-10

    This question already has an answer here: ViewPager PagerAdapter not updating the View 21 answers I am using ViewPager to allow user to swipe between its views. Is there a way how to force this ViewPager to reload/re-instantiate its views in case tha

  • Update data in ListFragment as part of ViewPager 2011-09-11

    I'm using the v4 compatibility ViewPager in Android. My FragmentActivity has a bunch of data which is to be displayed in different ways on different pages in my ViewPager. So far I just have 3 instances of the same ListFragment, but in the future I w

  • Is it possible to have a ViewPager inside of a ScrollView? 2011-09-11

    I'm trying to use a ViewPager inside of a ScrollView, but the ViewPager does not appear. If I remove the ScrollView the ViewPager appears fine. I've created a simple test project with the following: main.xml layout: <?xml version="1.0" encoding="u

  • Set default page for ViewPager in Android 2011-09-13

    I am using the following code, MAX is 2 pages. By default the position is 0 and adds a new page to the right. I inflate two layout files. How can I show the page1 when the app starts and add a new page to the left ? Thanks. main.xml <?xml version=

  • Replace Fragment inside a ViewPager 2011-10-11

    I'm trying to use Fragment with a ViewPager using the FragmentPagerAdapter. What I'm looking for to achieve is to replace a fragment, positioned in the first page of the ViewPager, with another one. The pager is composed by two pages. The first one i

  • ViewPager not showing correct View 2011-10-13

    I'm using the V4 compatibility JAR. I have a ViewPager that shows information about some bands. When I create the PagerAdapter, I pass it an ArrayList of Band objects. In the emulator, it always shows shows the 2nd band in the ArrayList's information

  • how to get access of my button in one of my viewpager pages? 2011-10-14

    I'm new to this android and java. My viewpager now can slide all my xml layout, but i don't know how to access item in them. I tried using Button mybutton = (Button) findViewById (R.layout.result), then set the onClickListener in mybutton, but then i

  • Changing ViewPager to enable infinite page scrolling 2011-10-14

    Jon Willis has posted on how to enable an infinite scrolling with his code. In there he said that he made some changes in the ViewPager class int the android support library. Which changes have been made and how is it possible to "recompile" the libr

  • Android ViewPager children not shown when instantiated using a composite widget rather than an inflated layout 2011-10-23

    I have an interesting quirk that I can't figure my way around. So I wrapped my layout around a widget so I could add some events and other convenient methods. However, using the constructor of the widget, I am unable to instantiate it as a child in a

  • Access TextView.setText() within Viewpager from onResume() method 2011-11-01

    I'm fairly new to Android development so I ran into a problem with the onResume() method. My code crashes when I try to write Name.setText() from within the onResume() method. Writing the TextView from within the onPause() method works surprisingly w

  • Add / Delete pages to ViewPager dynamically 2011-11-09

    I would like to add or delete pages from my view pager dynamically. Is that possible? --------------Solutions------------- Yes, since ViewPager gets the child Views from a PagerAdapter, you can add new pages / delete pages on that, and call .notifyDa

  • How do you get the current page number of a ViewPager for Android? 2011-11-14

    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 partic

  • ViewPager inside a ScrollView does not scroll correclty 2011-12-05

    I have a 'page' that has a number of components on it, and who's content is longer than the height of the device. Fine, just put all of the layout (the entire page) inside a ScrollView, no problem. One of the components is a ViewPager. This renders c

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.151 (s). 11 q(s)