Dont allow viewpager load two page in the first time

☆樱花仙子☆ 提交于 2019-12-22 12:23:20

问题


At the moment, i have a viewpager,when page scroll,i have load api for get new information. But in the first load viewpager,progress loading display two twice because it load first page and next page. I only want to load the first page in viewpager. How must I do?


回答1:


best way is to create one interface. and implement this interface to all fragment. e.g.

public interface FragmentInterface {    
    void fragmentBecameVisible();
}
  • in ur viewpager activity put like this e.g

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
    
            @Override
            public void onPageSelected(int position){
                    FragmentInterface fragment = (FragmentInterface) mAdapter.instantiateItem(viewPager, position); 
                if (fragment != null) {
                    fragment.fragmentBecameVisible();
                } 
    
                actionBar.setSelectedNavigationItem(position);
    
            }
    
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2){
            }
    
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    
  • in your fragment like

    public class TestFragmet extends Fragment implements FragmentInterface {
    
        @Override
        public void fragmentBecameVisible() {
        // your task to execute
        }
    
    }
    



回答2:


Put OffScreenPageLimit to ZERO. It won't load other fragments.

viewPager.setOffscreenPageLimit(0);


来源:https://stackoverflow.com/questions/26942304/dont-allow-viewpager-load-two-page-in-the-first-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!