Android two ways to add tab to Viewpager+fragment

Source: Internet
Author: User

The use of Viewpager in Android development is extensive, and it is not only possible to implement a simple start-up page, but also to combine fragment and add tab as a tab or to achieve a powerful smooth swipe for displaying large volumes of pages

Here are two ways to add tab to Viewpager+fragment :

The first type :

Define a Viewpager in the mainactivity layout

Declare Viewpager in mainactivity and implement it adapter inherit from Fragmentpageradapter

The first step is to get the Fragmentmanager object by overriding the constructed method of the argument and override a series of methods:

1. In GetItem, by switch to determine the current Viewpager on the first few pages, return the corresponding fragment

2.getCount the simplest, directly return the number of pages you want to create Viewpager

3. Override the Getpagetitle method to display some data on the label with its return value (you need to set the displayed data yourself by adapter calling the method when creating the label)

4. Get the Actionbar object in Mainactivity, create a label through Action.newtab, and add a label to Viewpager through the AddTab method of the action ( Add the corresponding number of labels by using the GetCount method of the adapter in the for loop as a condition

5. Finally realize the one by one correspondence between Viewpager and tab by implementing Viewpager and Tab monitoring events respectively.

 Mviewpager.setonpagechangelistener (new   Viewpager.simpleonpagechangelistener () {@Override  public  void  onpageselected ( position) {Act    Ionbar.setselectednavigationitem (position); }});
 Actionbar.newtab (). Settablistener ( Actionbar.tablistener () {@Override  public  void   ontabselected (Actionbar.tab Tab, fragmenttransaction ft) {mviewpager.se    Tcurrentitem (Tab.getposition ()); } @Override  public  void   ontabunselected (Actionbar.tab Tab, fragmenttransaction ft) {} @Override  p Ublic  void   ontabreselected ( Actionbar.tab Tab, fragmenttransaction ft) {}});  

The second type :

Store a viewpager in the collectiondemoactivity layout and display the label at the top (need to be written in the Viewpager layout)

Declare Viewpager in collectiondemoactivity and implement it adapter inherit from Fragmentstatepageradapter

The first step is to get the Fragmentmanager object by overriding the constructed method of the argument and override a series of methods:

1. Bind a fragment in GetItem and pass it through the bundle, and eventually return to the fragment object

2. Ibid.

3. If the top label of the Viewpager is defined in the layout, you need to override the Getpagetitle method here to display some data on the label with its return value

4. In the fragment layout that is bound in GetItem, define the layout you want to display and get the values passed to the bundle in fragment by getarguments

Note :The difference between Fragmentpageradapter and Fragmentstatepageradapter

1. Fragment objects created by inheriting Fragmentpageradapter are permanently stored in memory, so they are typically used to display a small number of pager pages, such as tabs

2. Inheritance Fragmentstatepageradapter will only retain up to three pager pages (that is, the current page, the previous and the next, if the current page is the first, there will only be two), so it is often used to display more pager pages, Like the pager page associated with the ListView

Android two ways to add tab to Viewpager+fragment

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.