The use of Tablayout provided by the Android Design library

Source: Internet
Author: User

In development, we often need to use Viewpager in conjunction with fragment, such as:

We can use the three-way open source Pagerslidingtabstrip to achieve, or viewpagerindicator, I generally favor the former. Now we can use the tablayout of the design support library to implement it. The final:

Creating layouts

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:app="Http://schemas.android.com/apk/res-auto"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <!--app:tabmode="scrollable"This property I set in the code-<!--tablayout.settabmode (tablayout.mode_scrollable);--> <android.support.design.widget.TabLayout Android:id="@+id/sliding_tabs"Android:layout_width="match_parent"Android:layout_height="wrap_content"/> <Android.support.v4.view.ViewPager Android:id="@+id/viewpager"Android:layout_width="match_parent"Android:layout_height="0px"Android:layout_weight="1"Android:background="@android: Color/white"/></linearlayout>add Tablayout in XML, like Viewpager, directly ' android.support.design.widget.TabLayout '. There are other properties that I'll set in the code. 
Create fragment

Package Me.chenfuduo.myfragmentdemo.fragment;import Android.os.bundle;import Android.support.annotation.nullable;import Android.support.v4.app.fragment;import Android.view.LayoutInflater; Import Android.view.view;import Android.view.viewgroup;import Android.widget.textview;import ME.CHENFUDUO.MYFRAGMENTDEMO.R;/** * Created by Administrator on 2015/7/30.*/ Public classPagefragment extends Fragment { Public StaticFinal String arg_page ="Arg_page"; Private intMPage;  Public StaticPagefragment newinstance (intpage) {Bundle args=NewBundle ();        Args.putint (Arg_page, PAGE); Pagefragment pagefragment=Newpagefragment ();        Pagefragment.setarguments (args); returnpagefragment; } @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); MPage=getarguments (). GetInt (Arg_page); } @Nullable @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View= Inflater.inflate (R.layout.fragment_page, container,false); TextView TextView=(TextView) view; Textview.settext ("Fragment #"+mpage); returnview; }}

Where fragment's layout is:

<?xml version="1.0"encoding="Utf-8"? ><textview xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:gravity="Center"/>
Adapter for Viewpager
Package Me.chenfuduo.myfragmentdemo.adapter;import Android.content.context;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;import me.chenfuduo.myfragmentdemo.fragment.PageFragment;/** * Created by Administrator on 2015/7/30.*/ Public classSimplefragmentpageradapter extends Fragmentpageradapter {finalintPage_count =3; PrivateString tabtitles[] =Newstring[]{"TAB1","tab2","tab3"}; Privatecontext Context;  Publicsimplefragmentpageradapter (Fragmentmanager fm,context Context) {super (FM);  This. Context =context; } @Override PublicFragment GetItem (intposition) {        returnPagefragment.newinstance (position +1); } @Override Public intGetCount () {returnPage_count; } @Override PublicCharsequence Getpagetitle (intposition) {        returnTabtitles[position]; }}
Set Tablayout
Package Me.chenfuduo.myfragmentdemo;import Android.os.bundle;import android.support.design.widget.TabLayout; Import Android.support.v4.app.fragmentactivity;import Android.support.v4.view.viewpager;import Android.view.Menu; Import Android.view.menuitem;import me.chenfuduo.myfragmentdemo.adapter.SimpleFragmentPagerAdapter; Public classThirdactivity extends Fragmentactivity {PrivateSimplefragmentpageradapter Pageradapter; PrivateViewpager Viewpager; PrivateTablayout tablayout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_third); Pageradapter=NewSimplefragmentpageradapter (Getsupportfragmentmanager (), This); Viewpager=(Viewpager) Findviewbyid (R.id.viewpager);        Viewpager.setadapter (Pageradapter); Tablayout=(tablayout) Findviewbyid (r.id.sliding_tabs);        Tablayout.setupwithviewpager (Viewpager);    Tablayout.settabmode (tablayout.mode_fixed); }}

Here are a few things I have to ask.

    • Tablayout.settabmode (tablayout.mode_fixed);

The beginning I set is:

Tablayout.settabmode (tablayout.mode_scrollable);
After running, three tablayout tags were squeezed into one piece. As follows:

Check the API to find the results. This tabmode has two attribute values:

    • mode_fixed:fixed tabs Display all tabs concurrently and is best used with content this benefits from quick pivots between Tabs.

    • mode_scrollable:scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larg ER number of tabs.

Without too much explanation, mode_scrollable is suitable for many tabs situations.

    • Setupwithviewpager must be called after Viewpager.setadapter ()

Setupwithviewpager must be called after Viewpager.setadapter ()

Look under the source code will know:

 Public void Setupwithviewpager (Viewpager viewpager) {    = viewpager.getadapter ();     if NULL         {thrownew illegalargumentexception ("Viewpager does not has a Pageradapter set");     Else  {        ...    }}

The above is the most basic usage, is not very simple. Ha ~

Http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0731/3247.html

The use of tablayout provided by the Android Design library

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.