Android app uses fragment examples in Viewpager to explain _android

Source: Internet
Author: User

It is said that the most recommended Android is the use of fragment in Viewpager, that is, Viewpager in the page is not like the previous use Layoutinflater directly from the layout file loading, but fragment. Pay attention to the fragment here.

It's the fragment in the Android.support.v4.view bag, not the fragment in the Android.app bag.

The activity of the fragment in the V4 package must be inherited from the fragmentactivity.

In fact, using fragment is very similar to not using fragment in front of you:

The first step is to place a viewpager component in the main layout file

The second step is to create a layout file for each page and write the interface.

The third step is to create a new fragment class for each page and load the interface in the layout file

The fourth part for Viewpager set adapter, but here adapter is not pageradapter, but replaced by

Fragmentpageradapter, implement two methods:

GetCount (): Returns the number of pages

GetItem (position): Returns the fragment of the position position.

Here is a viewpager and fragment to achieve the page sliding effect example:

First Inherit fragmentactivity,
Provide Viewpager with fragment and Fragmentpageradapter for presentation:
Fragment to specify the layout and functionality of the page

Fragment 
Private class Myfragment extends fragment { 
 
  private String text; 
  private int color; 
 
  Public Myfragment (String text, int color) { 
    this.text = text; 
    This.color = color; 
  } 
 
  @Override public 
  View Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle Savedinstancestate) { 
    TextView TV = new TextView (mainactivity.this); 
    Tv.setbackgroundcolor (color); 
    Tv.settext (text); 
    return TV; 
  } 
} 

adapter specifies how many pages the Viewpager has and which page the location needs to display:

Adapter 
  Private Class Myadapter extends Fragmentpageradapter {public 
    myadapter (fragmentmanager FM) { 
      super (FM); 
    } 
 
    @Override public 
    int GetCount () {return 
      pages.size (); 
    } 
 
    @Override public 
    Fragment getitem (int arg0) {return 
      pages.get (arg0); 
    } 
  } 

Set Onpagechangelistener to specify what additional actions are required when the page changes:

@Override public 
void onpagescrollstatechanged (int arg0) { 
 
} 
 
@Override public 
void onpagescrolled ( int arg0, float arg1, int arg2) { 
  linearlayout.layoutparams LP = (android.widget.LinearLayout.LayoutParams) Tabline.getlayoutparams (); 
  Lp.leftmargin = (int) ((arg0 + arg1) * mtablinewidth); 
  TABLINE.SETLAYOUTPARAMS (LP); 
} 
 
@Override public 
void onpageselected (int arg0) { 
  //Set titles for 
  (int i = 0; i < titles.size (); i++) { 
    if (arg0 = i) { 
      titles.get (i). SetSelected (True); 
    } else { 
      titles.get (i). SetSelected (false); 
  } 
} 

Complete code:

Package Com.hzy.myviewpager; 
 
Import java.util.ArrayList; 
Import Android.graphics.Color; 
Import Android.os.Bundle; 
Import android.support.annotation.Nullable; 
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.ViewPager; 
Import Android.support.v4.view.ViewPager.OnPageChangeListener; 
Import Android.util.DisplayMetrics; 
Import Android.view.Display; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.view.View.OnClickListener; 
Import Android.view.ViewGroup.LayoutParams; 
Import Android.widget.LinearLayout; 
 
Import Android.widget.TextView; 
 
Import com.hzy.myviewpager.r.id;  public class Mainactivity extends Fragmentactivity implements Onpagechangelistener, Onclicklistener {Viewpager pager 
  = NULL; 
  View tabline = null; private int MtablInewidth; 
  Titles TextView title1 = null; 
  TextView title2 = null; 
 
  TextView title3 = null; 
  Arraylist<textview> titles = null; 
 
  arraylist<fragment> pages = null; 
    @Override protected void OnCreate (Bundle arg0) {super.oncreate (arg0); 
    Initview (); 
  Inittabline (); 
    private void Initview () {Setcontentview (r.layout.activity_main); 
    pages = new arraylist<fragment> (); 
 
    Titles = new arraylist<textview> (); 
    Pager = (Viewpager) Findviewbyid (Id.main_viewpager); 
    Title1 = (TextView) Findviewbyid (ID.MAIN_TAB1); 
    Title2 = (TextView) Findviewbyid (ID.MAIN_TAB2); 
 
    Title3 = (TextView) Findviewbyid (ID.MAIN_TAB3); 
    Title1.setonclicklistener (this); 
    Title2.setonclicklistener (this); 
 
    Title3.setonclicklistener (this); 
    Titles.add (TITLE1); 
    Titles.add (Title2); 
 
    Titles.add (TITLE3); 
    Create new Fragments Pages.Add (New Myfragment ("Tab1", Color.Blue)); Pages.Add (New Myfragment ("TaB2", color.red)); 
 
    Pages.Add (New Myfragment ("Tab3", Color.cyan)); 
    Set Adapter Pager.setadapter (New Myadapter (Getsupportfragmentmanager ())); 
    Pager.setonpagechangelistener (this); 
    Pager.setcurrentitem (0); 
  Titles.get (0). SetSelected (True); 
    }//Tablines private void Inittabline () {tabline = Findviewbyid (id.main_tab_line); 
    Display display = GetWindow (). Getwindowmanager (). Getdefaultdisplay (); 
    Displaymetrics outmetrics = new Displaymetrics (); 
    Display.getmetrics (Outmetrics); 
    Mtablinewidth = OUTMETRICS.WIDTHPIXELS/3; 
    Layoutparams LP = Tabline.getlayoutparams (); 
    Lp.width = Mtablinewidth; 
  TABLINE.SETLAYOUTPARAMS (LP); @Override public void OnClick (View v) {switch (V.getid ()) {case Id.main_tab1:pager.setCurren 
      TItem (0, true); 
    Break 
      Case Id.main_tab2:pager.setCurrentItem (1, true); 
    Break Case Id.main_tab3:pager.setCurrentIteM (2, true); 
    Break 
    Default:break; @Override public void onpagescrollstatechanged (int arg0) {} @Override public void Onpagescrol LEDs (int arg0, float arg1, int arg2) {linearlayout.layoutparams LP = (android.widget.LinearLayout.LayoutParams) tabli 
    Ne.getlayoutparams (); 
    Lp.leftmargin = (int) ((arg0 + arg1) * mtablinewidth); 
  TABLINE.SETLAYOUTPARAMS (LP); @Override public void onpageselected (int arg0) {//Set titles for (int i = 0; i < titles.size (); I 
      + +) {if (arg0 = i) {titles.get (i). SetSelected (True); 
      else {titles.get (i). SetSelected (false); 
    }}//Fragment private class Myfragment extends fragment {private String text; 
 
    private int color; 
      Public Myfragment (String text, int color) {this.text = text; 
    This.color = color; @Override public View Oncreateview (Layoutinflater inflater, @NullaBle viewgroup container, @Nullable Bundle savedinstancestate) {TextView TV = new TextView (mainactivity.this); 
      Tv.setbackgroundcolor (color); 
      Tv.settext (text); 
    return TV;  }///Adapter private class Myadapter extends Fragmentpageradapter {public myadapter (fragmentmanager FM) 
    {super (FM); 
    @Override public int GetCount () {return pages.size (); 
    @Override public Fragment getitem (int arg0) {return pages.get (arg0); 
 } 
  } 
}

The location of the page indicator is manually set by implementing the Onpagechangelistener interface in the code.

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.