Viewpager Offscreen Page Limit

ViewPager 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.

My goal is to only download images when a fragment is selected, or only when the user is hovering the image.

Then load your grid with placeholder images, and do not load the real images until the page is changed.

Also, note that "hover" implies some sort of mouse or similar sort of pointer, which is not used on most Android devices.

ViewPager.setOffscreenPageLimit(0) doesn't work as expected

Does ViewPager require a minimum of 1 offscreen pages

Yes. If I am reading the source code correctly, you should be getting a warning about this in LogCat, something like:

Requested offscreen page limit 0 too small; defaulting to 1

ViewPager 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.

My goal is to only download images when a fragment is selected, or only when the user is hovering the image.

Then load your grid with placeholder images, and do not load the real images until the page is changed.

Also, note that "hover" implies some sort of mouse or similar sort of pointer, which is not used on most Android devices.

How does offScreenPageLimit work for viewpager

The source code for ViewPager is really the best place to find your answer.

In a nutshell, a ViewPager keeps an internal list of items (that respresent 'pages'). The number of items in this list is based on the the mOffScreenPageLimit value. By default it's set to 1, but you can change it by calling setOffscreenPageLimit(int limit).

In the (package protected) method populate(), which is where the ViewPager gets populated with pages, the following code snippet is located:

final int pageLimit = mOffscreenPageLimit;
final int startPos = Math.max(0, mCurItem - pageLimit);
final int N = mAdapter.getCount();
final int endPos = Math.min(N-1, mCurItem + pageLimit);

That determines the bounds of the dataset that backs the PagerAdapter and hence which positions pages will be requested for.

Non-existing pages are created using the (package protected) addNewItem() method, which basically ends up calling instatiateItem() on the associated PagerAdapter. This is the part you usually implement, either directly or through one of the concrete subclasses.

If something changes along the way, i.e. when the backing dataset is changed, or when setOffscreenPageLimit() is called again with a different value, items may be removed from the internal list, and a call destroyItem() on the PagerAdapter usually follows.

Does that answer your question? If not, try to be a bit more specific than a one-liner - that should help us to better help you. :)



Related Topics



Leave a reply



Submit