Viewpager how to get the current fragment (using Fragmentpageradapter)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.