Anonymous Anonymous - 1 year ago 194
Android Question

Android - ViewPager Adapter, set primary item before adapter is instantiated

I know i can do


to set my view pager at the desired position. My question is if and how i can do this before the adapter is instantiated.

Meaning that if I do

pagerAdapter = new ViewPagerAdapter(arg1,arg2....);

item 0 is first build and after that the item at the desired position is also build. Which takes double the time...In my adapter each item to be build is quite a lot of work so it would be best to avoid the 0 position item to be build if possible. Maybe by passing the desired position as an argument at the adapter's instantiation...?

Any way to do that?

Answer Source

You can fool the viewpager to start at a given position before the adapter is set, by calling onRestoreInstanceState, like this:

int currentItem = 5;

Parcel parcel = Parcel.obtain();
writeParcelable(BaseSavedState.EMPTY_STATE, 0);

writeParcelable(null, 0);

SavedState savedState = ViewPager.SavedState.CREATOR.createFromParcel(parcel);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download