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

时间:2022-01-11 21:46:46

(转载)http://*.com/questions/10073214/viewpager-setoffscreenpagelimit0-doesnt-work-as-expected?lq=1

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


解决方案:

The best way that I found was setUserVisibleHint
add this to your fragment

@Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { // load data here }else{ // fragment is no longer visible } }