5

I'm using a ViewPager together with a FragmentStatePagerAdapter, and I would like to launch an animation in a fragment when this fragment is visible and only when it is visible. The problem is that the animation starts when the fragment is the one after the one who is currently seen by the user. I moved the animation code from the "onActivityCreated" fragment function to the "onStart" fragment function, and even to the "onResume" fragment function, but the same thing happens.

Basically I need to wait for the fragment to be the page seen by the user to launch some code. How can I do that?

Thanks in advance.

thomaus
  • 5,780
  • 8
  • 42
  • 62
  • Confused a little... So the animation starts early? Before the Animation Fragment is called? – jnthnjns Jul 26 '12 at 16:00
  • Yes the animation starts too early. The animation starts as soon as the previous page of the one containing the animation is seen on screen. – thomaus Jul 26 '12 at 16:09

4 Answers4

8

I made it.

    CustomOnPageChangeListener page_listener = new CustomOnPageChangeListener();
    view_pager.setOnPageChangeListener(page_listener);

    ...

    private static class CustomOnPageChangeListener extends SimpleOnPageChangeListener
    {
        @Override
        public void onPageSelected(int position)
        {
            if (position == fragment_position)
            {
                 MyFragment fragment = (MyFragment) fragment_pager_adapter.instantiateItem(view_pager, fragment_position);
                 fragment.startAnimation();
            }

            super.onPageSelected(position);
        }
    }

and, of course, you must write a startAnimation() function that launches the animation into the MyFragment code.

thomaus
  • 5,780
  • 8
  • 42
  • 62
1

Have you tried using the following:

@Override
public void onWindowFocusChanged (boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   if (hasFocus)
      myTextView.startAnimation(anim);
}
jnthnjns
  • 8,894
  • 4
  • 39
  • 65
  • 1
    But this is for the activity. I'm looking a way to detect when a given Fragment is the one shown o screen by the ViewPager. – thomaus Jul 26 '12 at 16:10
  • Okay, then maybe you are looking for `ViewPager.OnPageChangeListener` then `onPageSlected` – jnthnjns Jul 26 '12 at 16:14
  • I just tried to use onPageSelected but then the problem is the same. How can I launch a function from the current page from this onPageSelected function??? – thomaus Jul 26 '12 at 16:51
1

You can override setUserVisibleHint inside your fragment

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (mAnimation == null)
        return;

    if (isVisibleToUser) {
        mAnimation.resumeAnimation();
    } else {
        mAnimation.pauseAnimation();
    }
}
Vlad
  • 768
  • 7
  • 17
0

Use this. Worked for FragmentStatePagerAdapter perfectly.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
  super.setUserVisibleHint(isVisibleToUser);
  if (isVisibleToUser) { 
      // TODO
  }
}
kaftanati
  • 480
  • 4
  • 5