0

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;
    }
}
Community
  • 1
  • 1
lis
  • 670
  • 1
  • 13
  • 32

1 Answers1

0

you need to work with your ViewPager's adapter. You need a setup like this....

// in your fragment
public boolean isActive(){
     return (mPageAdapter.getPosition() == MyFragment.POSITION);
}

// in your adapter
private pagePosition=0; // or initial position
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    pagePosition = position;
}
Jug6ernaut
  • 8,219
  • 2
  • 26
  • 26
  • when is the `onPageScrolled` Method called? I use a `ScreenSlidePagerAdapter extends FragmentStatePagerAdapter`(sorry, corrected it in question), eclipse says `The method onPageScrolled(int, float, int) from the type ArticlePageSliderActivity.ScreenSlidePagerAdapter is never used locally` (normaly it called me to add Override annotation).` Do I have the wrong adapter? (post it in question) – lis Jul 08 '13 at 06:04