I have a FragmentStatePagerAdapter with 6 fragments inside.
Each Fragment connect's to the server and loads data.
Right now, the server connection is being done in Fragment.onStart(), that means that at any moment, I have 3 http requests going (the selected Fragment, and one to each side).
What I want is to have only one connection at the time, so I figure to use
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(final int position)
{
CustomFragment fragment = (CustomFragment) myFragmentStatePagerAdapter.getItem(position);
fragment.onSelected();//do stuff in here
}
});
The thing is, getItem()
returns a new instance of the fragment, not yet added to the manager (and thus, not yet view created, etc).
Also, I've tried setUserVisibleHint(boolean isVisibleToUser)
but is not being call on visible, only on isVisibleToUser = false
So, how to achieve a "onPageSelected()" event for the Fragment?
Thanks in advance
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…