3

I am reusing the same fragment to display an image with an animation. The problem which I facing is the fragment view is loaded with the animation before it is visible to the user while swiping. Below is my PagerAdapter

public class PollsAdapter extends FragmentStatePagerAdapter {
/** Constructor of the class */
int clickPosition = 0;
ArrayList<Polls> pollsList = new ArrayList<Polls>();
public PollsAdapter(FragmentManager fm,ArrayList<Polls> pollsList) {
    super(fm);      
    this.pollsList=pollsList;
}

/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {     
    PollsFragment myFragment = new PollsFragment();
    Bundle data = new Bundle();     
    data.putSerializable("poll", pollsList.get(arg0));  
    data.putInt("position", arg0);  
    myFragment.setArguments(data);
    return myFragment;
    
}

/** Returns the number of pages */
@Override
public int getCount() {
    return pollsList.size();
}


public int getItemPosition(Object item) {       
        return POSITION_NONE;        
}   

}

This is how I set my viewPager

  pollsAdapter = new PollsAdapter(fm, pollsList);
    viewPager = (ViewPager) _rootView.findViewById(R.id.pager);
    viewPager.setAdapter(pollsAdapter);

I tried by using `viewPager.setOffscreenPageLimit(1); but this didn't work. I'm sure I miss something. Please help me to solve this problem.

Zain
  • 37,492
  • 7
  • 60
  • 84
LvN
  • 701
  • 1
  • 6
  • 22
  • What is your problem? Initialization of the fragment is expected behaviour. `ViewPager` must initialize the fragment to make it possible to partially display the fragment when you swipe to it. – dhke Mar 27 '15 at 14:37
  • @dhke As i said there is a small animation in the fragment that i load. It should be visible to the user only if the user sees my fragment. – LvN Mar 27 '15 at 14:48
  • Then the solution is to not play the animation when the fragment is loaded, but only when the fragment is displayed on screen. There is no way to stop ViewPager pre-loading a fragment for display. It *has* to do so to show the flip animation. As for getting notified which fragment is *current* in `ViewPager`, see here https://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager – dhke Mar 27 '15 at 14:53

1 Answers1

2

This is --unfortunately-- not possible. To display the flip animation, ViewPager has to pre-load at least the fragments to the left/right of the currently displayed fragment.

Because of this, the minimum possible value for ViewPager.setOffscreenPageLimit() is 1 as at least the direct neighbours need to be available.

In your case, the problem is rather that your fragment seems to be starting an animation as soon at is created.

In that case, you will need to change the starting logic for your animation. Your fragment can get notified by the ViewPager as soon as it is scrolled to (see ViewPager.OnPageChangeListener) for the mechanism). Note that the OnPageListener is not called when your activity is resumed so you also need to handle this case..

How to determine when Fragment becomes visible in ViewPager has more information on that topic.

dhke
  • 15,008
  • 2
  • 39
  • 56