Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the practical effect of viewPager.setOffscreenPageLimit(3);?

Tags:

android

I have three pages and have set Page Limit to 3.

  mViewPager.setOffscreenPageLimit(3);

What is the practical effect of this? Does it improve performance much or can it cause memory consumption issues?

like image 901
Code Droid Avatar asked Sep 21 '12 22:09

Code Droid


People also ask

What is ViewPager used for?

Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows. ViewPager is most often used in conjunction with android.

How to add ViewPager in activity in android?

You can't use ViewPager to swipe between Activities . You need to convert each of you five Activities into Fragments , then combine everything in one FragmentActivity with the Adapter you use with ViewPager .

What is Offscreen page Limit?

Is it possible to disable the offscreen page limit? No. It is already set to the minimum possible value: one page to each side of the viewed page. This is necessary to have the animation effects work -- you see parts of two fragments (original and new) at the same time.

How many times does mviewpager setoffscreenpagelimit (1) is called?

My FragmentStatePagerAdapter.getItem (int position) override function is called 3 times, which is what happens when I call mViewPager.setOffscreenPageLimit (1). I would expect it to only be called once, because I specified 0 offscreen pages.

How do I limit the number of pages per page in viewpager?

Alternatively, you can use the method setOffscreenPageLimit (int limit) provided by ViewPager to set how many page instances you want the system to keep in memory on either side of your current page. As a result, more memory will be consumed. So be careful when tweaking this setting if your pages have complex layouts.

What is the use of viewpager?

ViewPager is most often used along with fragment which is convenient way to manage the life cycle of each page. ViewPager class works with PagerAdapter which provides pages. There are three important steps to implement ViewPager: 1.) A layout (that contains ViewPager) for the MainActivity. 2.)

How to access the selected page within the viewpager at runtime?

We can access the selected page within the ViewPager at any time with the getCurrentItem method which returns the current page: The current page can also be changed programmatically with the With this getter and setter, we can easily access or modify the selected page at runtime.


2 Answers

Documentation : http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

If you use only three pages then you're still on the case the documentation describe, and it should allow your UI to be smoother. Of course it's consuming memory but as long as you stay in such a range it shouldn't be a problem.

like image 85
Morendo Avatar answered Oct 09 '22 20:10

Morendo


I have found that by dynamically change the number, not only can you optimize the paging animations and interactions by making the number large, you can also optimize the in page animations and interactions smooth by making the number small. (provided that you know the page length that you want)

(Just want to share this since it's relavent. Pretty sure the OP is not looking for an answer any more. :) )

like image 24
Edison Avatar answered Oct 09 '22 20:10

Edison