Android--tablayout

Source: Internet
Author: User
Tags xml attribute

Tabs make it easy-to-explore and switch between different views.

Through tablayout you can swipe or click to switch to a different page in one activity

First, this is a control in Google's material design, so you need to add a dependent library before using it.

    Compile ' com.android.support:design:26.0.0-alpha1 '

The specific use of the idea is using tablayout+viewpager+fragment: In short, Tablayout is the label bar, fragment is a tag corresponding to the content, Viewpager used to manage the combination of both, In other words, fragment can actually be replaced by other controls.

The first is the layout XML code for the main activity:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <Android.support.design.widget.TabLayoutAndroid:id= "@+id/tab_layout"Android:layout_width= "Wrap_content"Android:layout_height= "? Attr/actionbarsize">    </Android.support.design.widget.TabLayout>    <Android.support.v4.view.ViewPagerAndroid:id= "@+id/view_pager"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/></LinearLayout>

Then the Java code for the main activity:

 Public classMainactivityextendsappcompatactivity {PrivateTablayout tablayout; PrivateViewpager Viewpager; PrivateList<string> titlelist =NewArraylist<>(); PrivateList<fragment> fragmentlist =NewArraylist<>(); PrivateMyadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {Initviewpager ();    Inittablayout (); }    Private voidInitviewpager () {Viewpager=(Viewpager) Findviewbyid (R.id.view_pager); Fragmentlist.add (NewFragment1 ()); Fragmentlist.add (NewFragment2 ()); Fragmentlist.add (NewFragment3 ()); Titlelist.add (First); Titlelist.add (Second); Titlelist.add (Third); Adapter=NewMyadapter (Getsupportfragmentmanager (), titlelist,fragmentlist);    Viewpager.setadapter (adapter); }    Private voidinittablayout () {tablayout=(tablayout) Findviewbyid (r.id.tab_layout);    Tablayout.setupwithviewpager (Viewpager); }}

First create a new list of two lists for the label title and corresponding fragment

    Private New Arraylist<>();     Private New Arraylist<> ();

Then there's the binding on tags and fragment.

 private  void   Initviewpager () {viewpager  = (Viewpager) Findviewbyid (R.id.view_pager)        ;        Fragmentlist.add ( new   Fragment1 ());        Fragmentlist.add ( new   Fragment2 ());        Fragmentlist.add ( new   Fragment3 ());        Titlelist.add ( "First"  "second"  "third"  = new   Myadapter (        Getsupportfragmentmanager (), titlelist,fragmentlist);    Viewpager.setadapter (adapter); }

Here it is. The first is to find the Viewpager in the layout file by ID, then add the corresponding title and fragment in the two list, then need an adapter, the adapter needs to create a new class, Here the name of this class I set as Myadapter inherit from Fragmentpageradapter

 Public classMyadapterextendsFragmentpageradapter {PrivateList<string>List_title; PrivateList<fragment>list_fragment;  PublicMyadapter (Fragmentmanager Fragmentmanager, list<string> list_title, list<fragment>list_fragment) {        Super(Fragmentmanager);  This. List_title =List_title;  This. list_fragment =list_fragment; }     PublicFragment GetItem (inti) {        returnList_fragment.get (i); } @Override Public intGetCount () {returnlist_fragment.size (); } @Override PublicCharsequence Getpagetitle (intposition) {        returnList_title.get (position% list_title.size ());
}
}

The adapter also has two lists, and then requires a fragmentmanager in the constructor's three parameters, in addition to the two lists . the Fragment GetItem () method needs to return the corresponding contents according to the parameters, GetCount () needs to return the size of the content, Getpagetitle () returns the corresponding caption.

And then, to connect Tablayout with Viewpager.

    Private void inittablayout () {        = (tablayout) Findviewbyid (r.id.tab_layout);        Tablayout.setupwithviewpager (Viewpager);    }

As for the layout of fragment and Java code on a TextView code, look at the previous blog bar = =.

The effect is:

Tablayout XML attribute value (Source: HTTPS://JUEJIN.IM/ENTRY/589EC5B01B69E60059C5B9FB):

Properties meaning
Tabgravity Fill, fill Tablayout;center, center display
Tabmode Fixed, fixing label, scrollable, scrollable label, can use fixed when the number is small, if the label is outside the screen range, set to scrollable better
Tabmaxwidth The maximum width of the tab
Tabindicatorcolor The color of the bottom slide line, default is Coloraccent
Tabindicatorheight The height of the bottom slide line
tabpadding* Padding of the tab page
Tabbackground The background of the tab page
Tabtextappearance Text Settings
Tabselectedtextcolor Check Font Color

Android--tablayout

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.