When using Fragmentpageradapter, it is unavoidable to pass data between Mainactivity and the currently displayed fragment. But Fragmentpageradapter does not provide us with APIs similar to the Getcurrentfragment category.
Through the Fragmentpageradapter source, it is found that each fragment switchover will be called to the Fragmentpageradapter.setprimaryitem method. This method sets a fragment to Mcurrentprimaryitem. Obviously, Mcurrentprimaryitem is the current fragment we want to get. Then we just need to overload this method and record the incoming fragment.
Xx
We can define a Currentfragment member in adapter.
Public class extends fragmentpageradapter{fragment currentfragment; @Overridepublicvoidint = (qipanfragment) object; Super. Setprimaryitem (container, Position, object);}}
In the outer activity,
Getfragmentpageadapter (). currentfragment
Viewpager how to get the current fragment (using Fragmentpageradapter)