Android PagerAdapter, get current position

I want to get the current position of the visible view of my PagerAdapter

I have not seen an obvious function like getPosition() , and I want it.

I want to add an object to its arraylist in this position, but first I need to know it

+21
android arraylist android-viewpager
Dec 16 '11 at 17:41
source share
3 answers

Would you use:

 int position = mViewPager.getCurrentItem() 
+38
Dec 17 '11 at 17:50
source share

I had this problem and could not get getCurrentItem() .

As a result, I got the position from the ViewPager , and not from the PageAdapter . The onPageSelected(int currentPage) gets the current page to display.

 //custom PageAdapter implementation mAdapter = new AwesomePagerAdapter(); //Our custom view pager that extends from ViewPager mPager = (CustomViewPager) findViewById(R.id.preview_gallery); mPager.setAdapter(mAdapter); // get the item that we should be showing from the intent mCurrentPage = extra.getInt("currentIndex"); // show the item the user picked mPager.setCurrentItem(mCurrentPage); // listen for page changes so we can track the current index mPager.setOnPageChangeListener(new OnPageChangeListener() { public void onPageScrollStateChanged(int arg0) { } public void onPageScrolled(int arg0, float arg1, int arg2) { } public void onPageSelected(int currentPage) { //currentPage is the position that is currently displayed. } }); 

Doing this in PageAdaper did not work for me as I want to preload images that are not visible. The position that is passed to instantiateItem(View collection, int position) for PageAdapter` is the position of the next element initialized. This has nothing to do with the item being displayed.

+23
Dec 12
source share

https://github.com/Shereef/ViewPagerPlusExpandableList/blob/master/src/net/shereef/vewpagerplusexpandablelistexample/ViewPagerPlusExpandableListActivity.java#L204

if i write after this line

  Log.i("pager",myPager.getCurrentItem()+""); 

it will show in logcat the current page of the element during the start of oncreate, which is always 0

noteice I used the object for the viewpager myself, not the adapter.

+3
Jan 02 2018-12-21T00:
source share



All Articles