Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know i can do

 viewPager.setCurrentItem(position) 

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....);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(position);

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 needs quite a lot of work to be build, 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?

like image 530
Anonymous Avatar asked Sep 25 '14 22:09

Anonymous


4 Answers

If the time and work is what you're worry about, I'd try to avoid building the page at position 0 until the desired one has been built. You could use a variable lets say "desiredPageHasBeenBuilt" in your adapter, when requesting the item at position 0 you could return an "empty page" if the variable desiredPageHasBeenBuilt is false, when your desired page has been build set the variable to true and the page 0 can be built.

like image 123
kevinkl3 Avatar answered Oct 22 '22 23:10

kevinkl3


For a solution that works in Android M as well as older versions, use reflection as follows:

int currentItem = 5;

// Set initial position first...
Field field = ViewPager.class.getDeclaredField("mRestoredCurItem");
field.setAccessible(true);
field.set(mPager, currentItem);

// ...and then set adapter
mPager.setAdapter(adapter);

Using reflection is safe, because you control the ViewPager implementation (it's included with your app).

If you use Proguard, then you need to include the following in its config:

-keepclassmembers class android.support.v4.view.ViewPager {
    private int mRestoredCurItem;
}  

or the field mRestoredCurItem will be renamed by Proguard.

like image 3
Theo Lassonder Avatar answered Oct 22 '22 23:10

Theo Lassonder


You can just set blank adapter and after that set your real adapter this way you will "trick" the viewpager and you want load any data you dont want to.

this.viewPager.setAdapter(new PagerAdapter() {
        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return false;
        }
    });
this.viewPager.setCurrentItem(imagePosition, false);
this.viewPager.setAdapter(adapter);
this.viewPager.setCurrentItem(imagePosition, false);
like image 3
risto Avatar answered Oct 23 '22 00:10

risto


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

writeInt(currentItem);
writeParcelable(null, 0);

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

mPager.onRestoreInstanceState(savedState);

mPager.setAdapter(mAdapter);
like image 1
Claus Holst Avatar answered Oct 22 '22 23:10

Claus Holst