I suggest working with fragments (and not directly with views).
You need an interface on your snippets to tell them when they will be shown:
public interface IShowedFragment { public void onShowedFragment(); }
Make all your fragments implemented in this interface, and in this method call the loaders / asyncTasks / background tasks.
Then add onPageChangeListener to your ViewPager, and when you find that the user has changed the page, call the interface method on your fragment. You have a choice with this listener, with one of the methods that you can wait for the viewPager to stop sliding to invoke the interface call.
To get the correct fragment to make this call, take the fragment from your FragmentApadter.instantiateItem (ViewGroup, int), which will return the fragment for this position if it is already loaded.
mPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { Fragment fragment = (Fragment) mAdapter.instantiateItem(mPager, position); if(fragment instanceof IShowedFragment){ ((IShowedFragment) fragment).onShowedFragment(); } } (...)
Just as you can prepare your fragments with empty views, and when you click on one, you will start loading the data.
galex
source share