ViewPager offscreen page limit
Asked Answered
C

8

30

Is there a way to bypass the normal behavior of ViewPager and its offscreen page limit? My ViewPager contains four fragments, each containing a gridview of images. The problem I have is that on instansiation of the ViewPager, two fragments are created, which results in that about 20 images (about 10 per fragment) is downloaded/fetched from catch simultaneously. Is it possible to disable the offscreen page limit?

My goal is to only download images when a fragment is selected, or only when the user is hovering the image. One way to achieve this is to use the onPageSelected listener and set a flag, which tells the GridViewAdapter if it's allowed to download the image or not.

A second way that I can think of is to set a HoverListener on the ImageView, and only download the image on onHover, but that listener is only available in 4.0 and later.

Is there a better way to achieve this?

Criticize answered 25/7, 2012 at 12:52 Comment(0)
C
46

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.

Casie answered 25/7, 2012 at 13:16 Comment(2)
I have set the offscreen to 1 but the fragment that remains off screen is a google support map fragment after being created the first time when i go back the map comes up and overlaps my fragment, any ideas as why?Wheeled
@IgnacioGarat: The offscreen limit should have nothing to do with that. Sounds like something is not working correctly with your PagerAdapter.Casie
Y
34

Simply set the offscreen limit to one.

ViewPager mViewpager = (ViewPager)findView....
mViewPager.setOffscreenPageLimit(1);
Yean answered 25/7, 2012 at 12:58 Comment(0)
C
5

My goal is to only download images when a fragment is selected

You can use setUserVisibleHint(boolean isVisibleToUser) callback of the Fragment in your ViewPager.

Carcassonne answered 18/5, 2016 at 9:36 Comment(2)
setUserVisibleHint is deprecated now, any other option?Hamitic
You may find a response here https://mcmap.net/q/369994/-in-androidx-fragment-app-fragment-setuservisiblehint-is-deprecated-and-not-executed-whyCarcassonne
T
2

Handle viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()

Inside it, remove extra views after position + 1 and before position - 1 using viewPager.removeViewAt(int) (i.e. limit the pager to have only 3 pages loaded: current, previous and next.

Then you should handle destroyItem in the viewpager adapter, recycle()ing the bitmaps in the destroyed view.

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    LinearLayout layout = (LinearLayout) object;
    ((ViewPager) container).removeView(layout);
    ImageView imgDisplay = (ImageView) layout.findViewById(R.id.quranPage);
    Drawable drawable = imgDisplay.getDrawable();
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();
        if (bitmap != null) //when reading page fails, this will be null
            bitmap.recycle();
    }
}
Taper answered 1/11, 2015 at 3:20 Comment(0)
D
2

You can also do this in xml using Android Data Binding:

<android.support.v4.view.ViewPager
    [...]
    app:offscreenPageLimit="@{1}"
    [...]
</android.support.v4.view.ViewPager>
Dreda answered 16/11, 2016 at 7:43 Comment(0)
H
1

To avoid downloading the images without the user actually scrolling to the fragment you can override "setUserVisibilityHint (boolean visibleToUser)" and load your images only if "visibleToUser" becomes true

Hydrate answered 12/9, 2017 at 0:57 Comment(0)
T
0

mViewPager.setOffscreenPageLimit(1); wont give what you want,it will load neighbor fragment if its not load already(it means it will load 2 fragment).

Lets say if you want to load all your fragment(4 fragment) at once, then only use setOffscreenPageLimit(3), else avoid using setOffscreenPageLimit.

I think you cannot change the default behavior of viewpager loading neighbor fragment.

Thanos answered 13/7, 2017 at 13:18 Comment(0)
S
0

the offscreen page limit can only be larger than 0, or #OFFSCREEN_PAGE_LIMIT_DEFAULT.

reference here

Skiascope answered 9/7, 2023 at 14:32 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewKareenkarel

© 2022 - 2024 — McMap. All rights reserved.