viliam
viliam

Reputation: 141

Viewpager set current item async

i have View pager with 1000 items placed in fragment. When fragment is create i need to move viewPager to 600th item. below is code how i actually do that, but it takes some time.

mPager.post(new Runnable() {
    public void run() {
        mPager.setCurrentItem(600, false);
    }
}); 

Is it some way to make it async/smoother ?

Thanks.

Upvotes: 0

Views: 523

Answers (2)

Arslan Ashraf
Arslan Ashraf

Reputation: 867

As you cannot touch UI elements from background thread so what you can do to speed it up is to load more item at once like setting the ViewPager.offScreenPageLimit().

For example I am setting 100 but you can set more and test it then:

mPager.setOffscreenPageLimit(100);

Also use true in setCurrentItem() to make it smoother. Like:

mPager.post(new Runnable() {
    public void run() {
        mPager.setCurrentItem(600, true);
    }
});

Upvotes: 4

cgr
cgr

Reputation: 4636

Arslan answer is good.
Also, if you are using FragmentStatePagerAdapter, be aware of issues mentioned here https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=37990.
These happens especially when you have offScreenPageLimit() mentioned less than (NO_OF_ITEMS - 1). These issues occur when user swipes as the StatePagerAdapter deletes the fragment when it is no more required and creates it back when it is required.The indexing is not taken properly in this adapter.

Upvotes: 0

Related Questions