87

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 particular page.

I used viewPager.setCurrentItem(1); for setting the first item.

Is there a similar method for getting the current page?

JDJ
  • 4,298
  • 3
  • 25
  • 44
sam_k
  • 5,983
  • 14
  • 76
  • 110

8 Answers8

182

in the latest packages you can also use

vp.getCurrentItem()

or

vp is the viewPager ,

pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);

you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.

private int currentPage;

    private static class PageListener extends SimpleOnPageChangeListener{
            public void onPageSelected(int position) {
                Log.i(TAG, "page selected " + position);
                   currentPage = position;
        }
    }
Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • Thanks a lot man..its works..Have you worked before on ViewPager? – sam_k Nov 14 '11 at 05:42
  • @Necronet no, it shouldn't. The PageListener class should be an inner class, not a nested class. – dcow Nov 15 '13 at 21:04
  • I got null pointer exception sometimes in the beginning of the pager images, for more reliable solutions i suggest this, it works fine for me. http://stackoverflow.com/a/13831450/2296787 – MBH Feb 24 '15 at 20:58
  • `setOnPageChangeListener()` is deprecated, use `addOnPageChangeListener()` instead. – ZzZombo Sep 07 '19 at 13:40
47

There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager

Flexo
  • 87,323
  • 22
  • 191
  • 272
Krishna Shrestha
  • 1,662
  • 14
  • 38
  • I actually posted this answer first @ChuckBoris, still this one seems to have gathered more votes and apparently an endorsement. Strange are the ways of this site :) – devanshu_kaushik Apr 16 '16 at 18:27
17

If you only want the position, vp.getCurrentItem() will give it to you, no need to apply the onPageChangeListener() for that purpose alone.

devanshu_kaushik
  • 929
  • 11
  • 30
11

You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:

ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
       if(position == 1){  // if you want the second page, for example
           //Your code here
       }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});
Geraldo Neto
  • 3,670
  • 1
  • 30
  • 33
7

For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution

1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...

Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {
             //Toast.makeText(MyActivity.this, i+"  Is Selected  "+data.size(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageSelected( int i) {
          // here you will get the position of selected page
            maintain = i;


        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });


updateButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(MyActivity.this, i+"  Is Selected  "+data.size(), Toast.LENGTH_SHORT).show();
            data.set(maintain, "Replaced "+maintain);         
            myViewPager.getAdapter().notifyDataSetChanged();
        }
    });
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12
  • 1
    setOnPageChangeListener is deprecated. Use addOnPageChangeListener instead of setOnPageChangeListener. – Selin Jan 04 '16 at 13:03
6

getCurrentItem(), doesn't actually give the right position for the first and the last page I fixed it adding this code:

public void CalcPostion() {    
    current = viewPager.getCurrentItem();

    if ((last == current) && (current != 1) && (current != 0)) {
        current = current + 1;
        viewPager.setCurrentItem(current);
    }
    if ((last == 1) && (current == 1)) {
        last = 0;
        current = 0;
    }
    display();
    last = current;
}
Taryn
  • 242,637
  • 56
  • 362
  • 405
Da-monk
  • 95
  • 1
  • 8
6

The setOnPageChangeListener() method is deprecated. Use addOnPageChangeListener(OnPageChangeListener) instead.

You can use OnPageChangeListener and getting the position inside onPageSelected() method, this is an example:

   viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Log.d(TAG, "my position is : " + position); 
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

Or just use getCurrentItem() to get the real position:

viewPager.getCurrentItem();
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
5

There is no any method getCurrentItem() in viewpager.i already checked the API

raman
  • 337
  • 3
  • 13