4

I have a viewpager. This viewpager can have dynamic number of pages depends on server and user can start with any page they want. So I programmed like below..

mViewPager.setAdapter(mAdapter);
mViewPager.setCurrentItem(mStartPosition);

mStartPosition is a position to start..

My question is here. This view pager always load pages where 0, 1 and mStartPosition and nearby pages loaded sequentially. If mStartPosition is 10, then loading 0, 1, and load 9,10,11. It slow down performance and waste data.

I want just start with mStartPosition and nearby page. Help me~!

Lee Jeongmin
  • 793
  • 11
  • 22
  • 1
    *If mStartPosition is 10, then loading 0, 1, and load 9,10,11.*, where did you read it? Did you change `setOffscreenPageLimit`? – Blackbelt Mar 18 '15 at 09:10
  • @Lee Jeongmin: You are wrong in anticipating the behavious of ViewPager. It by default cache 1 page i.e. if you want to display 10 it will create 9 and 11 page. It might have to scroll to page 10 which will definitely create performance issue. The same behavior can be observed in listview. – Roll no1 Mar 18 '15 at 09:17
  • @Rollno1I know that loading 9,10,11 is correct behavior. but i don't need 0,1 pages..if i start with position 10. – Lee Jeongmin Mar 19 '15 at 04:23
  • @Blackbelt I didn't set setOffScreenPageLimit. – Lee Jeongmin Mar 19 '15 at 04:24
  • I have same issue. Still see page 0,1 before page 9,10 load. – Trung Nguyen Sep 14 '15 at 07:22
  • @LeeJeongmin Did you find any solution? I found this topic https://stackoverflow.com/questions/26049223/android-viewpager-adapter-set-primary-item-before-adapter-is-instantiated but there is no pretty solution. – AppiDevo Oct 04 '17 at 12:43
  • @AppiDevo It was too far from now..So my memory could not be correct. Anyway, It would be working differently with the version of support library. Try with the lastest support library. I tested this again with support library 26.1.0 and I found it worked fine. – Lee Jeongmin Oct 08 '17 at 09:32

2 Answers2

3

setCurrentItem(int item) sets current item with smooth animated transition.

You just need to use mViewPager.setCurrentItem(mStartPosition, false);

Then it will only load 9,10,11

Sohaib
  • 10,941
  • 9
  • 32
  • 34
  • 1
    I tested it. but still load 0, 1 pages. – Lee Jeongmin Mar 19 '15 at 04:25
  • Alright, I tested on two devices nexus4 & 5. Using android.support.v4.view.ViewPager, after setting the adapter I call mViewPager.setCurrentItem(10,false) and in instantiateItem I put a log statement and I only see 10,9,11 being logged. Are you sure you are calling setCurrentItem once? – Sohaib Mar 19 '15 at 04:45
  • It depends when you are calling `setAdapter` and `setCurrentItem` – Phil Aug 05 '16 at 09:53
2

It's Android limitation so 0,1, 10,9,11 is expected if you are setting the adapter after the pager has been layout-ed. Otherwise you will get 10, 9, 11

Long answer: Check the source code. In the source code (at least in support-v4-23.4.0) when populating the pager with an adapter mViewPager.setAdapter(mAdapter); the pager will always go to position 0 because it's member mCurItem is 0. And on the next line when you call mViewPager.setCurrentItem(mStartPosition, false); it will change the mCurItem. If you try to call mViewPager.setCurrentItem(mStartPosition, false); before mViewPager.setAdapter(mAdapter); in order to set mCurItem it won't work again as the adapter in still null and won't work as it will return before setting. But if you set the adapter before layouting the pager, a specific property in it mFirstLayout won't cause population right away and by the time it does the mCurItem will be set.

So basically: Use those calls before you are layouting the pager.

Phil
  • 1,200
  • 13
  • 17