Viewpager the latest way to implement page cards--Concise tablayout (Google support Pack)

Source: Internet
Author: User

:

To Add a dependency package:
    ‘com.android.support:design:22.2.0‘
Layout file:
<?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  =;     <android.support.design.widget.TabLayoutandroid:id="@+id/tabs"android: Layout_width="Match_parent"android:layout_height="Wrap_content"app: Tabindicatorcolor="@color/red"app:tabselectedtextcolor="@color/red"  App:tabtextcolor="@color/black"/>                                                    <!--sliding layout content--    <android.support.v4.view.ViewPagerandroid:id="@+id/vp_view"android: Layout_width="Match_parent"android:layout_height="wrap_content"/>                        </linearlayout>
page code:
 Public  class fiveactivity extends Activity {    PrivateTablayout mtablayout;PrivateViewpager Mviewpager;PrivateLayoutinflater Minflater;PrivateList<string> mtitlelist =NewArraylist<> ();//Page Card header collection    PrivateView View1, View2, VIEW3, View4, view5;//Page card view    PrivateList<view> mviewlist =NewArraylist<> ();//Page card View collection    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.activity_five);        Mviewpager = (Viewpager) Findviewbyid (R.id.vp_view);        Mtablayout = (tablayout) Findviewbyid (r.id.tabs); Minflater = Layoutinflater.from ( This); View1 = Minflater.inflate (R.layout.activity_main,NULL); View2 = Minflater.inflate (R.layout.activity_main,NULL); VIEW3 = Minflater.inflate (R.layout.activity_main,NULL); VIEW4 = Minflater.inflate (R.layout.activity_main,NULL); VIEW5 = Minflater.inflate (R.layout.activity_main,NULL);//Add a page card viewMviewlist.add (View1);        Mviewlist.add (VIEW2);        Mviewlist.add (VIEW3);        Mviewlist.add (VIEW4); Mviewlist.add (VIEW5);//Add a page card titleMtitlelist.add ("No:1"); Mtitlelist.add ("No:2"); Mtitlelist.add ("No:3"); Mtitlelist.add ("No:4"); Mtitlelist.add ("No:5"); Mtablayout.settabmode (tablayout.mode_fixed);//Set tab mode, current system default modeMtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (0)));//Add tab tabMtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (1))); Mtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (2))); Mtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (3))); Mtablayout.addtab (Mtablayout.newtab (). SetText (Mtitlelist.get (4))); Mypageradapter Madapter =NewMypageradapter (mviewlist); Mviewpager.setadapter (Madapter);//Set adapter for ViewpagerMtablayout.setupwithviewpager (Mviewpager);//Associate Tablayout with Viewpager. Mtablayout.settabsfrompageradapter (Madapter);//Set adapter for tabs}//viewpager AdapterClass Mypageradapter extends Pageradapter {PrivateList<view> mviewlist; Public Mypageradapter(list<view> mviewlist) { This. mviewlist = mviewlist; }@Override         Public int GetCount() {returnMviewlist.size ();//Page card Count}@Override         Public Boolean Isviewfromobject(View view, Object object) {returnview = = object;//Official recommended wording}@Override         PublicObjectInstantiateitem(ViewGroup container,intPosition) {Container.addview (Mviewlist.get (position));//Add a page card            returnMviewlist.get (position); }@Override         Public void Destroyitem(ViewGroup container,intPosition, Object object) {Container.removeview (Mviewlist.get (position));//Delete a page card}@Override         PublicCharsequenceGetpagetitle(intPosition) {returnMtitlelist.get (position);//Page card title}    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Viewpager the latest way to implement page cards--Concise tablayout (Google support Pack)

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.