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

前端 未结 11 821
抹茶落季
抹茶落季 2020-11-22 13:02

The fragments I use in my ViewPager instance are quite resource intensive, so I\'d only like to load one at a time. When I try the following:

mV         


        
相关标签:
11条回答
  • 2020-11-22 13:38

    ViewPager is default to load the next page(Fragment) which you can't change by setOffscreenPageLimit(0). But you can do something to hack. You can implement onPageSelected function in Activity containing the ViewPager. In the next Fragment(which you don't want to load), you write a function let's say showViewContent() where you put in all resource consuming init code and do nothing before onResume() method. Then call showViewContent() function inside onPageSelected. Hope this will help.

    0 讨论(0)
  • 2020-11-22 13:45

    Use This // create boolean for fetching data private boolean isViewShown = false;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getView() != null) {
            isViewShown = true;
            // fetchdata() contains logic to show data when page is selected mostly asynctask to fill the data
            fetchData();
        } else {
            isViewShown = false;
        }
    } 
    
    0 讨论(0)
  • 2020-11-22 13:46

    You can try like this :

    public abstract class LazyFragment extends Fragment {
        protected boolean isVisible;
        /**
         * 在这里实现Fragment数据的缓加载.
         * @param isVisibleToUser
         */
        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if(getUserVisibleHint()) {
                isVisible = true;
                onVisible();
            } else {
                isVisible = false;
                onInvisible();
            }
        }
        protected void onVisible(){
            lazyLoad();
        }
        protected abstract void lazyLoad();
        protected void onInvisible(){}

    protected abstract void lazyLoad();
    protected void onInvisible(){}
    
    0 讨论(0)
  • 2020-11-22 13:47

    Please Try This Code for resolve issue of refreshing view in Viewpager....

    /* DO NOT FORGET! The ViewPager requires at least “1” minimum OffscreenPageLimit */
    int limit = (mAdapter.getCount() > 1 ? mAdapter.getCount() - 1 : 1);
    mViewPager.setOffscreenPageLimit(limit);
    
    0 讨论(0)
  • 2020-11-22 13:51

    this may be old thread but this seems to work for me. Override this function :

    @Override
    public void setMenuVisibility(boolean menuVisible) { 
        super.setMenuVisibility(menuVisible);
    
        if ( menuVisible ) {
            /**
             * Load your stuffs here.
             */
        } else  { 
            /**
             * Fragment not currently Visible.
             */
        } 
    }
    

    happy codings...

    0 讨论(0)
  • 2020-11-22 13:52

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题