user1437481
user1437481

Reputation: 470

Android Viewpager Performance

I am currently using a ViewPager with a FragmentStatePager adapter, with approx 25 different fragments (and different relativelayouts). When I scroll I generally notice substantial lag when swiping to some views, except when I declare viewpager.setoffscreenpagelimit(25). Is there a way to increase performance of this viewpager, without having all the pages loaded at all time?

Upvotes: 2

Views: 7633

Answers (2)

StErMi
StErMi

Reputation: 5469

Can you please add more detail to your question? For example, what are you doing in each pages?

Have you updated to latest Android Support Library?

Changes for v4 support library:

  • Improved interaction behavior for ViewPager.
  • Fixed a bug that could cause ViewPager to select the wrong page.
  • Fixed use of removeView() method during layout for ViewPager.
  • Fixed issue with SearchViewCompat where using the back button to dismiss does not clear the search text. This fix only applies to host API levels 14 and higher.

You have always to remember that FragmentStatePagerAdapter auto-save pages and remove them when not used. So if you swipe very fast there will be a lot of load/unload of pages. You can try to increase the amount of pages loaded in memory.

Upvotes: 1

Simon Zettervall
Simon Zettervall

Reputation: 1784

Using the ViewPager out of the box will load a maximum of three Fragments/Views. Stay with that number. It is not the ViewPager that is slow, you are doing something in your code that makes it slow. Does any of your Fragment do any blocking or CPU-consuming task? For instance loading Bitmaps? Are you having some parsing that could be done in an AsyncTask? Maybe you could put such things else where and save it to a database and then when you load the Frament it will stay very clean and efficient if reading from a database.

Upvotes: 3

Related Questions