Basic use of viewpager+fragment in Android

Source: Internet
Author: User

These days learned the basic use of Viewpager+fragement and wrote a demo. Now put the code on and on.

    1. The first is the layout file

 <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 "    tools:context=". Mainactivity " >        <!-- viewpager Components  -->     <android.support.v4.view.ViewPager         Android:id= "@+id/viewpager"         android:layout_width= "Match_parent"         android:layout_height= "Match_parent" >                        <!-- pagertabstrip is the toggle effect of the tab page  -->        < android.support.v4.view.pagertabstrip&Nbsp;           android:id= "@+id/pagertab"                android:layout_width= "Wrap_content"               android:layout_height= " Wrap_content "              />                </ android.support.v4.view.viewpager>    </relativelayout><!--Precautions:          1. Here Viewpager and  pagertabstrip all have to write the name of the package, otherwise it will classnotfount         2.api said: In the layout of the XML Pagertabstrip as a sub-label Viewpager to use, can not be taken out, or will be error      -->

2.mainactivity.java

Mainactivity The main thing to do is to load the control and instantiate fragment, the focus is to note that myviewpageradapter this adapter's internal class.

Package com.xlp.myviewpagerfragment;import java.util.arraylist;import android.os.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  android.view.menu;import android.view.menuitem;public class mainactivity extends  fragmentactivity {     private viewpager m_vp;      //  you can set the caption properties by Pagertabstrip      private PagerTabStrip  pagertabstrip;     private fragment1 mfragment1;      private Fragment2 mfragment2;     private Fragment3  mfragment3;     //  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 ();  } public void initview ()  {       m_vp =  (Viewpager)  findviewbyid (R.id.viewpager);       pagerTabStrip =  (Pagertabstrip)  findviewbyid (r.id.pagertab);       //  set the underline color        Pagertabstrip.settabindicatorcolor (Getresources (). GetColor (         Android. R.color.holo_green_dark));       pagertabsTrip.setbackgroundcolor (Getresources (). GetColor (        android. R.color.holo_red_dark));       mfragment1 = new fragment1 ();       mfragment2 = new fragment2 ();       Mfragment3 = new fragment3 ();       fragmentlist = new  ArrayList<Fragment>       fragmentlist.add (mfragment1);       fragmentlist.add (Mfragment2);       Fragmentlist.add (MFRAGMENT3);       titlelist.add ("first page");       titlelist.add ("second page");       titlelist.add ("third page");       m_vp.setadapter (New myviewpageradapter (Getsupportfragmentmanager ())); }      public class&nbsP myviewpageradapter extends fragmentpageradapter {           public myviewpageradapter (FRAGMENTMANAGER&NBSP;FM)  {      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SUPER (FM);      }        @Override       public fragment getitem (int arg0)  {           return fragmentlist.get (arg0);       }       @Override        public int getcount ()  {            return fragmentlist.size ();      }        @Override       public charsequence getpagetitle (int  Position)  {           // todo auto-generated method stub            return titlelist.get (position);       } }      @Override      public  Boolean oncreateoptionsmenu (Menu menu)  {      // inflate  the menu; this adds items to the action bar if it  is present.      getmenuinflater (). Inflate (R.menu.main, menu);       return true;     }       @Override      public boolean onoptionsitemselected (MenuItem item )  {      // Handle action bar item clicks  here. the action bar will      // automatically handle clicks on  the home/up button, so long      // as you  specify a parent activity in androidmanifest.xml.       Int id = item.getitemid ();      if  (id ==  R.id.action_settings)  {       return true;       }      return super.onoptionsitemselected (item);      }}

3.fragment1.java

One of the fragment pages, where only one is shown, the other is the same

 package com.xlp.myviewpagerfragment;import android.os.Bundle;import  Android.support.v4.app.fragment;import android.util.log;import android.view.layoutinflater;import  android.view.View;import android.view.ViewGroup;public class Fragment1 extends  fragment { private view mmainview;  @Override  public void oncreate ( Bundle savedinstancestate)  {  // TODO Auto-generated method stub   super.oncreate (savedinstancestate); &NBSP;&NBSP;LOG.I ("Xlp",  "Fragment1-->oncreate ()");   //dynamically loading layout files   layoutinflater inflater = getactivity (). Getlayoutinflater ();   mmainview = inflater.inflate (r.layout.fragment1,     (ViewGroup)   Getactivity (). Findviewbyid (R.id.viewpager),  false); }  @Override  public View  Oncreateview (Layoutinflater inflater, viewgroup container,   bundle savedinstancestate)  {  log.i ("Xlp",  " Fragment1-->oncreateview () ";  viewgroup viewgroup =  (ViewGroup)   Mmainview.getparent ();  return mmainview; }  @Override  public void  OnDestroy ()  {  // TODO Auto-generated method stub   Super.ondestroy (); &NBSP;&NBSP;LOG.V ("Xlp",  "Fragment1-->ondestroy ()"); }  @Override   Public void onpause ()  {  // TODO Auto-generated method stub   super.onpause (); &NBSP;&NBSP;LOG.V ("Xlp",  "Fragment1-->onpause ()"); }  @Override   Public void onresume ()  {  // todo auto-generated method stub   super.onresume (); &NBSP;&NBSP;LOG.V ("Xlp",  "Fragment1-->onresume ()"); }  @Override  public void onstart () &NBSP;{&NBSP;&NBSp;// todo auto-generated method stub  super.onstart (); &NBSP;&NBSP;LOG.V ("Xlp",   "Fragment1-->onstart ()"); }  @Override  public void onstop ()  {   // todo auto-generated method stub  super.onstop (); &NBSP;&NBSP;LOG.V ("Xlp" ,  "Fragment1-->onstop ()");  }}

4. Finally, put

Basic use of viewpager+fragment in Android

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.