问题
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