is it possible to call a method in a fragment of a viewPager only, when it's the active page?
or maybe it's possible to different, if the fragment is the current page, by a boolean in the fragment? (like: if(isActivePage)
).
I want to load some data in an asynctask and show a ProgressDialog
, when loading. But only for the active page (user should be able to adjust, that the fragments are cached, which he visit, not the page on the left and right side too...) viewPager.setOffscreenPageLimit(0)
doesn't work (look this post), I tried to set an OnPageChangeListener
but you can't manipulate any attributes of the actually fragment in there.
For notice, here what I tried in FragmentActivity
in onCreate
:
...
fragments = new ArrayList<ArticlePageFragment>(pages);
for (int i = 0; i < pages; i++)
{
ArticlePageFragment fragment = new ArticlePageFragment();
Bundle args = new Bundle();
...
fragment.setArguments(args);
fragments.add(fragment);
}
viewPager.setOnPageChangeListener(new OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
if (position > 0)
fragments.get(position - 1).isActivePage = false;
if (position < pages)
fragments.get(position + 1).isActivePage = false;
fragments.get(position).isActivePage = true;
}
...
}
(in ArticlePageFragment
isActivePage
is only a public boolean...)
if it's impossible, maybe there's another way, without ViewPager
or ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
EDIT
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
public ScreenSlidePagerAdapter(FragmentManager fm)
{
super(fm);
}
public Fragment getItem(int position)
{
return fragments.get(position);
}
@Override
public int getCount()
{
return pages;
}
}