Viewpager+fragment Foundation

Source: Internet
Author: User
Tags getcolor

Viewpager+fragment


Mainactivity.java

package wuxiaocheng.cn.activity;import android.app.bundle;import  android.support.v4.app.fragment;import android.support.v4.app.fragmentactivity;import  android.support.v4.app.fragmentmanager;import android.support.v4.app.fragmentpageradapter;import  android.support.v4.view.pagertabstrip;import android.support.v4.view.viewpager;import  java.util.arraylist;public class mainactivity extends fragmentactivity {     private ViewPager mvp;    /**  you can set the caption properties by Pagertabstrip  */     private pagertabstrip mpts;    private fragmentone  one;       //first Page     private fragmenttwo  two;                 Second page of    //    private FragmentThree three;                      //Third page     /**  page list  */    private  ArrayList<Fragment> fragmentList;    /**  Title List  */     private ArrayList<String> titleList = new ArrayList<String> () ;     @Override     protected void oncreate (Bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         setcontentview (R.layout.activity_main);         initview ();     }    private void  initview ()  {        mvp =  (Viewpager)   Findviewbyid (R.id.id_viewpager);         mpts =  (Pagertabstrip)  findviewbyid (R.id.id_pagertab);         //Setting the underline color          Mpts.settabindicatorcolor (Getresources (). GetColor (Android. R.color.holo_green_dark));         mpts.setbackgroundcolor (getResources (). GetColor (Android. R.color.holo_red_dark));         one = new fragmentone () ;         two = new fragmenttwo ();         three = new fragmentthree ();         fragmentList = new ArrayList<Fragment> ();         fragmentlist.add (one);         fragmentlist.add (one);         fragmentliSt.add (three);         titlelist.add ("one");         titlelist.add ("two");         titlelist.add ("three");         mvp.setadapter (New myviewpageradapter ( Getsupportfragmentmanager ()));    }    /**      *  internal class definition adapter      */    public class  myviewpageradapter extends fragmentpageradapter {         public myviewpageradapter (FRAGMENTMANAGER&NBSP;FM)  {        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SUPER (FM);        }         /**         *  get each page           */        @Override        public Fragment  GetItem (int arg0)  {           return  Fragmentlist.get (arg0);       }        Total number of  /**         *  pages           */         @Override          public int getcount ()  {             return fragmentlist.size ();         }         /**         *   title         */       of each page    @Override         public charsequence getpagetitle (int position)  {             return titlelist.get (position);         }    }}

Fragmentone.java

and a Fragmenttwo.java.

and Fragmentthree.java

Inside the code is similar, I will send one on the line, others to change the layout file on the line

Fragmentone.java

Package Wuxiaocheng.cn.activity;import Android.app.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import android.view.view;import android.view.viewgroup;/** * Created by Administrator on 2015/7/17. */public class Fragmentone extends Fragment {@Override public View oncreateview (Layoutinflater inflater, ViewGroup        Container,bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_one, NULL);    return view; }}

Here is the layout file

Activity_main.xml

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"      Android:layout_width= "Match_parent"     android:layout_height= "Match_parent"      android:orientation= "vertical" >    <!-- viewpager Components-->     <android.support.v4.view.viewpager        android : id= "@+id/id_viewpager"         android:layout_height= "Wrap_content"         android:layout_width= "Wrap_content" >         <!-- pagertabstrip is the toggle effect of the tab page-->         <android.support.v4.view.PagerTabStrip             android:id= "@+id/id_pagertab"              android:layout_height= "Wrap_Content "            android:layout_width=" Wrap_ Content "/>    </android.support.v4.view.viewpager></linearlayout>

The layout file adds its own display content

Three pages I'll just send one.

This is my example.

Fragment_one.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "android:gravity=" center "android:layout_width=" match_parent "android:layout_height=" Match_parent "> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= " First page "android:textsize=" 30SP "/></linearlayout>

Run the following effect

Viewpager+fragment Foundation

Related Article

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.