"Practice" Viewpager label swipe

Source: Internet
Author: User

Layout:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <Android.support.v4.view.ViewPager Android:id= "@+id/viewpager"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Android.support.v4.view.PagerTabStrip Android:id= "@+id/pagertab"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" ></android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager> </RelativeLayout>
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "android:layout_width=" match_parent "    android:layout _height= "Match_parent" >    <TextView        android:id= "@+id/fragment_textview"         Android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content"/></ Linearlayout>

Java code

 Public classMainactivityextendsappcompatactivity {PrivateViewpager Viewpager; PrivatePagertabstrip Pagertab; PrivateList<string> titles;//label    PrivateList<fragment>fragments; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Initialize pageInitview (); //initializing an array of headersInittitle (); //Initialize Fragmentsinitfragment (); Fragmentadapter Adapter=NewFragmentadapter (Getsupportfragmentmanager ());    Viewpager.setadapter (adapter); }    Private voidInitview () {Viewpager=(Viewpager) Findviewbyid (R.id.viewpager); Pagertab=(Pagertabstrip) Findviewbyid (R.id.pagertab);        Pagertab.setbackgroundcolor (Color.green);        Pagertab.settabindicatorcolor (color.red); Pagertab.setdrawfullunderline (true); }    Private voidInittitle () {titles=NewArraylist<string>();  for(inti = 0;i<10;i++) {Titles.add ("First" +i+ "tags"); }    }    Private voidinitfragment () {Fragments=NewArraylist<fragment>();  for(inti = 0;i<titles.size (); i++) {myfragment myfragment=Newmyfragment (); Bundle Bundle=NewBundle (); Bundle.putstring ("Content", "first" +i+ "Fragment");            Myfragment.setarguments (bundle);        Fragments.add (myfragment); }    }    classFragmentadapterextendsfragmentpageradapter{ PublicFragmentadapter (fragmentmanager FM) {Super(FM); } @Override PublicFragment GetItem (intposition) {            returnFragments.get (position); } @Override Public intGetCount () {returnfragments.size (); } @Override PublicCharsequence Getpagetitle (intposition) {            returnTitles.get (position); }    }}
 Public classMyfragmentextendsFragment {PrivateString content; PrivateTextView Fragment_textview; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Bundle Bundle=getarguments (); Content= bundle.getstring ("Content", "No contents"); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View= Inflater.inflate (R.layout.myfragment,container,false); Fragment_textview=(TextView) View.findviewbyid (R.id.fragment_textview); returnview; } @Override Public voidonactivitycreated (Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate);    Fragment_textview.settext (content); }}

"Practice" Viewpager label swipe

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.