ViewPager offscreen page limit

前端 未结 7 2048
慢半拍i
慢半拍i 2020-11-30 04:45

Is there a way to bypass the normal behavior of ViewPager and its offscreen page limit? My ViewPager contains four fragments,

相关标签:
7条回答
  • 2020-11-30 05:16

    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.

    0 讨论(0)
  • 2020-11-30 05:17

    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.

    0 讨论(0)
  • 2020-11-30 05:20

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

    <android.support.v4.view.ViewPager
        [...]
        app:offscreenPageLimit="@{1}"
        [...]
    </android.support.v4.view.ViewPager>
    
    0 讨论(0)
  • 2020-11-30 05:21

    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();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:21

    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.

    0 讨论(0)
  • 2020-11-30 05:27

    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

    0 讨论(0)
提交回复
热议问题