android - How to implement a ViewPager with different Fragments / Layouts -


when start activity implements viewpager, viewpager created various fragments. want use different layouts each fragment, problem viewpager shows 2 layouts @ max (second layout on of remaining fragments after 1).

here code swipeactivity implements viewpager :

public class swipeactivity extends fragmentactivity {      mypageadapter pageadapter;      @override     protected void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_swipe);         pageadapter = new mypageadapter(getsupportfragmentmanager());         viewpager pager=(viewpager)findviewbyid(r.id.pager);         pager.setadapter(pageadapter);         actionbar bar = getactionbar();         bar.setdisplayhomeasupenabled(true);     }     /**     * custom page adapter     */     private class mypageadapter extends fragmentpageradapter     {         public mypageadapter(fragmentmanager fm)         {             super(fm);         }         @override         public int getcount()         {             return 5;         }         @override         public fragment getitem(int position)         {             switch(position)             {                 case 0: return new myfragment();                 case 1: return secondfragment.newinstance("asdasd");                 default : return ramfragment.newinstance("s");             }         }      } } 

here code fragments

public class myfragment extends fragment {    @override    public view oncreateview(layoutinflater paramlayoutinflater, viewgroup paramviewgroup,    bundle parambundle)    {      return paramlayoutinflater.inflate(r.layout.processorlayout, paramviewgroup, false);    } } 

i used 5 fragments this, having different layouts, viewpager shows 2 @ max.

edit : code secondfragment

public class secondfragment extends fragment {    public static final string extra_message = "extra_message";    public static final secondfragment newinstance(string paramstring)   {     secondfragment f = new secondfragment();     bundle localbundle = new bundle(1);     localbundle.putstring("extra_message", paramstring);     f.setarguments(localbundle);     return f;   }    @override   public view oncreateview(layoutinflater paramlayoutinflater, viewgroup paramviewgroup, bundle parambundle)   {      return paramlayoutinflater.inflate(r.layout.motherboardlayout, paramviewgroup, false);   } } 

as asked question, wanted take time , effort explain viewpager multiple fragments , layouts in detail. here go.

viewpager multiple fragments , layout files - how to

the following complete example of how implement viewpager different fragment types , different layout files.

in case, have 3 fragment classes, , different layout file each class. in order keep things simple, fragment-layouts differ in background color. of course, layout-file can used fragments.

firstfragment.java has orange background layout, secondfragment.java has green background layout , thirdfragment.java has red background layout. furthermore, each fragment displays different text, depending on class , instance is.

also aware using support-library's fragment: android.support.v4.app.fragment

mainactivity.java (initializes viewpager , has adapter inner class). again have @ imports. using android.support.v4 package.

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.viewpager;  public class mainactivity extends fragmentactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);               viewpager pager = (viewpager) findviewbyid(r.id.viewpager);         pager.setadapter(new mypageradapter(getsupportfragmentmanager()));     }      private class mypageradapter extends fragmentpageradapter {          public mypageradapter(fragmentmanager fm) {             super(fm);         }          @override         public fragment getitem(int pos) {             switch(pos) {              case 0: return firstfragment.newinstance("firstfragment, instance 1");             case 1: return secondfragment.newinstance("secondfragment, instance 1");             case 2: return thirdfragment.newinstance("thirdfragment, instance 1");             case 3: return thirdfragment.newinstance("thirdfragment, instance 2");             case 4: return thirdfragment.newinstance("thirdfragment, instance 3");             default: return thirdfragment.newinstance("thirdfragment, default");             }         }          @override         public int getcount() {             return 5;         }            } } 

activity_main.xml (the mainactivitys .xml file) - simple layout file, containing viewpager fills whole screen.

<android.support.v4.view.viewpager     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/viewpager"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     /> 

the fragment classes, firstfragment.java import android.support.v4.app.fragment;

public class firstfragment extends fragment {      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         view v = inflater.inflate(r.layout.first_frag, container, false);          textview tv = (textview) v.findviewbyid(r.id.tvfragfirst);         tv.settext(getarguments().getstring("msg"));          return v;     }      public static firstfragment newinstance(string text) {          firstfragment f = new firstfragment();         bundle b = new bundle();         b.putstring("msg", text);          f.setarguments(b);          return f;     } } 

first_frag.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@android:color/holo_orange_dark" >      <textview         android:id="@+id/tvfragfirst"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_centervertical="true"         android:textsize="26dp"         android:text="textview" /> </relativelayout> 

secondfragment.java

public class secondfragment extends fragment {  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view v = inflater.inflate(r.layout.second_frag, container, false);      textview tv = (textview) v.findviewbyid(r.id.tvfragsecond);     tv.settext(getarguments().getstring("msg"));      return v; }  public static secondfragment newinstance(string text) {      secondfragment f = new secondfragment();     bundle b = new bundle();     b.putstring("msg", text);      f.setarguments(b);      return f; } } 

second_frag.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@android:color/holo_green_dark" >      <textview         android:id="@+id/tvfragsecond"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_centervertical="true"         android:textsize="26dp"         android:text="textview" />  </relativelayout> 

thirdfragment.java

public class thirdfragment extends fragment {  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view v = inflater.inflate(r.layout.third_frag, container, false);      textview tv = (textview) v.findviewbyid(r.id.tvfragthird);           tv.settext(getarguments().getstring("msg"));      return v; }  public static thirdfragment newinstance(string text) {      thirdfragment f = new thirdfragment();     bundle b = new bundle();     b.putstring("msg", text);      f.setarguments(b);      return f; } } 

third_frag.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@android:color/holo_red_light" >      <textview         android:id="@+id/tvfragthird"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_centervertical="true"         android:textsize="26dp"         android:text="textview" />  </relativelayout> 

the end result following:

the viewpager holds 5 fragments, fragments 1 of type firstfragment, , displays first_frag.xml layout, fragment 2 of type secondfragment , displays second_frag.xml, , fragment 3-5 of type thirdfragment , display third_frag.xml.

enter image description here

above can see 5 fragments between can switched via swipe left or right. 1 fragment can displayed @ same time of course.

last not least:

i recommend use empty constructor in each of fragment classes.

instead of handing on potential parameters via constructor, use newinstance(...) method , bundle handing on parameters.

this way if detached , re-attached object state can stored through arguments. bundles attached intents.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -