What I Have
I have a ViewPager with 5 fragments. I want to animate some TextViews inside the fragments whenever they become visible to the user.
I can't use onResume() as the fragments to the left and right are already created. I can't use setUserVisibilityHint() as it is called before onCreateView() so the views are not ready yet.
So what should be the way to animate the views whenever a particular fragment becomes visible to the user?
I'm not sure, but if you say that
setUserVisibilityHint
calls beforeonCreateView
, than check view on null here (make reference on view - field), and if it not null - animate it. Also animate it always inonCreateView
.You're right on (1) and (2). However,
setUserVisibilityHint()
gets called Once Again with a True value after the Fragment comes to Front on Display. But on First Run the Fragment to be shown gets itssetUserVisibilityHint()
called beforeonCreateView()
.SOL: You should use the above said behaviour of
setUserVisibilityHint()
along withonResume()
to animate the views whenever a particular fragment becomes visible to the user.setUserVisibilityHint(boolean isVisibleToUser)
gets called with True param value. But as the Fragment's State is not Resumed we postpone and let theonResume()
handle animation.setUserVisibilityHint(boolean isVisibleToUser)
will get called with True param it they come on to Display. Here you check for the Fragment Animated or not and Do animation.CODE
a) Declare two Global Boolean Fields:
isAnimated
andisOnDisplay
a.1) Set
isAnimated
boolean to True;b) Override
setUserVisibilityHint(boolean isVisibleToUser)
:Here you set
isOnDisplay
boolean toisVisibleToUser
and check is the Fragment Not Already Animated and is in Resumed State and is Visible to User.c) Override
onResume()
Check if the Fragment Not Already Animated and is Visible to User.
I know this answer might be a bit late, but I hope it can help others in a similar situation.
You could use FragmentViewPager library (I am the author), which deals with the issue you are facing for you. Its features are:
Fragment
pages to get notified when they are actually visible/invisible to the userFragmentViewPagers
(nesting)A basic usage would be:
FragmentViewPager
programmatically or via XML to an Activity or Fragment, as you would with nativeViewPager
FragmentViewPager
's adapter. Your adapter should inheritcom.sbrukhanda.fragmentviewpager.adapters.FragmentPagerAdapter
orcom.sbrukhanda.fragmentviewpager.adapters.FragmentStatePagerAdapter
Override
onResumeFragments()
of the hosting Activity and callFragmentViewPager.notifyPagerVisible()
:or
onResume()
of the hosting Fragment and callFragmentViewPager.notifyPagerVisible()
:Override
onPause()
of the hosting Activity or Fragment and callFragmentViewPager.notifyPagerInvisible()
:FragmentVisibilityListener
on all Fragment pages that you wish to receive callbacks for their visibility stateIf you wish to see a more complete sample code, then check project's sample project.
If you want to do it in individual fragments, then you can use
isVisible()
for each fragment in your fragment transition and create a listener. Whenever a fragment will become visible , listener will be invoked and each fragment will implement that listener and do your intended task in the overridden method.