22


I know that by default PagerAdapter loads only the current, next and previous pages. Is there any way to change it, so it will load each and every page? Thanks!

RE6
  • 2,684
  • 4
  • 31
  • 57

4 Answers4

48

If you have N pages, you can use setOffscreenPageLimit(N-1), so that it will keep all pages in memory.

UgglyNoodle
  • 3,017
  • 1
  • 21
  • 17
  • Worked like a charm. Just a question, why N-1? That shows that no matter what, there is a solution for EVERYTHING! – RE6 Oct 03 '12 at 19:23
  • The argument tells the adapter how many pages on each side of the current page to keep in memory. For example, if the argument was 2, then it would keep up to 5 pages in memory: the current page, two on the left, and two on the right. – UgglyNoodle Oct 04 '12 at 04:21
  • It would be even more usefull if it created pages only if the user goes to that page and after that it kept that page. It can be heavy to create 6, 7 or more pages specially if your pages do a lot of work. – Cícero Moura Feb 27 '18 at 12:52
1

I'm not sure this is an answer, but the whole concept of an adapter (also for lists/grids, etc) is that you don't have all the Views loaded to the memory all the time, but it inflates only what's visible. Think about a ListView with 10000 items, the app would crash if it would try to load them all.

The documentation states somewhere that "setOffscreenPageLimit" can be useful and faster if you know how many pages and content you want to show. If you know the information to be shown and it is not heavy weight "setOffscreenPageLimit" would actually improve your performance because the views are only inflated once.

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
0

I'm not sure this is an answer, but the whole concept of an adapter (also for lists/grids, etc) is that you don't have all the Views loaded to the memory all the time, but it inflates only what's visible.

Think about a ListView with 10000 items, the app would crash if it would try to load them all.

With HorizontalScrollView you'll have all the views loaded to memory.

Givi
  • 3,283
  • 4
  • 20
  • 23
-9

I think it is not possible to load all pages at a time.You should redesign your code.

Rishabh Agrawal
  • 861
  • 2
  • 15
  • 25