Reputation: 90
In a fragment i'm using a ViewPager and setting a current item.
mViewPager.setCurrentItem(5);
mViewPager.setAdapter(someAdapter);
mViewPager.addOnPageChangeListener(this);
Now, OnPageChangeListener's method "onPageSelected(int position)" is not giving positon value 4 or 6, but giving 1. I want if current position is set to 10 then position parameter give 11.
Upvotes: 0
Views: 1148
Reputation: 611
Your are writing correct code but not in correct order.
mViewPager.setAdapter(someAdapter);
mViewPager.setCurrentItem(5);
mViewPager.addOnPageChangeListener(this);
you should first set the adapter
then tell which position to load in ViewPager
.
Upvotes: 1
Reputation: 2793
You could call setCurrentItem()
only after calling setAdapter()
and wait for the viewpager to complete its loading. There seems no proper way of doing this. A hack is to wait some milliseconds before calling setCurrentItem()
-
Reference: Android ViewPager setCurrentItem not working after onResume
An alternative is to call setCurrentItem()
in an OnLayoutChangeListener
-
Upvotes: 1