In Android applications, multiple-screen sliding is a very common style, not using Viewpager code implementation will be very long, if the use of Viewpager, the code will be much shorter, But the use of Viewpager also has drawbacks: need to import Android-support-v4.jar, details can not control. But now things are different, ANDROID-SUPPORT-V4 offers a lot of practical features, so that a new Android project will now import this jar package by default. Let's use Viewpager to do the sliding. Another concept is that fragment and Fragmentactivity,fragment are a special class that has a consistent life cycle and a view-consistent interface, that is, fragment is equal to a life-cycle view, but Note: Fragment is not a view, and it has no inheritance relationship with view. The advantage of using fragment is that fragment can be reused, and that each fragment can handle its own business as an activity, so that the coupling between modules is lower than the logic of writing all the business in an activity is much clearer. There is, because each module business is implemented within the fragment, so as long as the activity to manage several fragment on the line, do not need to do business-related things, and finally, fragment can be used to do different resolution models of adaptation. Fragment are available in the SDK (Android 3.0 and higher) and android-support-v4, but because of compatibility problems, we can only use the fragment in Android-support-v4, Unless you want your apk to run only on Android phones after 3.0, fragmentactivity is similar to fragment. About fragment and fragmentactivity, in fact, there are some basic usage needs to understand, but considering that fragment is not the focus of this article, so here is not introduced, in addition, this article only with the fragment to do a simple interface, we should be able to read, Okay, here we are.
Viewpager + Fragment often use the code is extracted from the actionbarsherlock, this effect is known to be sliding to switch. directly on the code.
Here's a brief description of Fragmentstatepageradapter and Fragmentpageradapter.
2 Adapter:
The first fragment state adapter-at present only the first 1 fragment present fragment and the next 1 fragment other destroys, suitable for loading multiple data;
The second type of fragmentpageradapter-all exists, so it is not suitable to load large amounts of data such as pictures or anything, easily memory overflow.
Engineering Structure:
1.activity adapter (static type Fragment)
public class Viewpagefragment extends Fragmentactivity {//This is how many fragment pages static final int num_items = 5;
Private Myadapter Madapter;
Private Viewpager Mpager;
private int nowpage;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.pagers_fragment_main);
Madapter = new Myadapter (Getsupportfragmentmanager ());
Mpager = (Viewpager) Findviewbyid (R.id.mypagers_pager);
Mpager.setadapter (Madapter); /** * Stateful, there will be only the first 3 existing other destroys, the first 1, the middle, the next/public static class Myadapter extends Fragmentstatepagerad
Apter {public myadapter (fragmentmanager FM) {super (FM);
@Override public int GetCount () {return num_items; //Get each item @Override public Fragment getitem (int position) {return arrayfragment.newinstance PO
Sition); //Initialize each page card option @Override publicObject Instantiateitem (viewgroup arg0, int arg1) {//TODO auto-generated method stub return Super.instantia
Teitem (arg0, arg1); @Override public void Destroyitem (ViewGroup container, int position, object object) {System.out.
println ("position destory" + position);
Super.destroyitem (container, Position, object); }/** * No state will be all loaded, this is suitable for a small number of particularly many pictures ah what or with Fragmentstatepageradapter * @author lilei * */ /public static class Myadapter extends Fragmentpageradapter {//Public myadapter (Fragmentmanager FM) {//SUP
ER (FM);
////@Override//public int GetCount () {//return num_items; ////@Override//Public Fragment getitem (int position) {////returns the corresponding Fragment//return ARRA
Yfragment.newinstance (position); ////@Override//public void Destroyitem (ViewGroup container, int position, object object) {// SYstem.out.println ("position destory" + position);
Super.destroyitem (container, Position, object);
}//}/** * All Fragment/public static class Arrayfragment extends Fragment {
int mnum;
static arrayfragment newinstance (int num) {arrayfragment array= new arrayfragment ();
Bundle args = new Bundle ();
Args.putint ("num", num);
Array.setarguments (args);
return array;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Mnum = getarguments ()!= null?
Getarguments (). GETINT ("num"): 1;
System.out.println ("mnum Fragment create =" + Mnum); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle saved
Instancestate) {System.out.println ("Oncreateview =");
The view view V = null that loads the display of each fragment here; if (Mnum = =0) {v = inflater.inflate (R.layout.pagers_fragment1, container, false);
((TextView) V.findviewbyid (R.id.textview1)). SetText (mnum+ "= Mnum");
}else if (mnum = = 1) {v = inflater.inflate (R.layout.pagers_fragment1, container, false);
((TextView) V.findviewbyid (R.id.textview1)). SetText (mnum+ "= Mnum");
}else if (mnum = = 2) {v = inflater.inflate (R.layout.pagers_fragment1, container, false);
((TextView) V.findviewbyid (R.id.textview1)). SetText (mnum+ "= Mnum");
}else{v = inflater.inflate (R.layout.pagers_fragment1, container, false);
((TextView) V.findviewbyid (R.id.textview1)). SetText (mnum+ "= Mnum");
} return v; @Override public void onactivitycreated (Bundle savedinstancestate) {System.out.println ("ONACTIVITYCR
eated = ");
Super.onactivitycreated (savedinstancestate);
@Override public void Ondestroyview () { System.out.println (Mnum + "mnumdestory");
Super.ondestroyview ();
@Override public void OnDestroy () {Super.ondestroy ();
}
}
}
2. And 1 is not much different (it's useful to see a person)
public class Viewpagefragmentcs extends Fragmentactivity {//This is how many fragment pages private Myadapter madapter;
Private Viewpager Mpager;
Private list<entity> List = new arraylist<viewpagefragmentcs.entity> ();;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.pagers_fragment_main);
for (int i = 0; i < 7; i++) {Entity ee = new Entity ();
Ee.name = "ll" + i;
Ee.age = "" + I;
List.add (EE);
} madapter = new Myadapter (Getsupportfragmentmanager (), list);
Mpager = (Viewpager) Findviewbyid (R.id.mypagers_pager);
Mpager.setadapter (Madapter);
Private class entity{public String name;
Public String age; //Here you can pass list<fragment> or list<object> data public class Myadapter extends Fragmentstatepag
eradapter {list<entity> list_ee; Public MyadapteR (Fragmentmanager FM, list<entity> EE) {super (FM);
This.list_ee = EE;
@Override public int GetCount () {return list_ee.size (); ///Initialize each page card option @Override public Object instantiateitem (viewgroup arg0, int position) {ARR
Ayfragment ff = (arrayfragment) super.instantiateitem (arg0, position);
Ff.setthings (List_ee.get (position), position);
return FF; @Override public void Destroyitem (ViewGroup container, int position, object object) {System.out.
println ("position destory" + position);
Super.destroyitem (container, Position, object); @Override public Fragment getitem (int arg0) {//TODO auto-generated a stub return
New Arrayfragment (); }/** * All Fragment/public class Arrayfragment extends Fragment {private En
Tity ee;
private int position; public voidSetthings (Entity ee,int position) {this.ee =ee;
This.position = position;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedinsta
Ncestate) {System.out.println ("Oncreateview =");
Here load each fragment display view View V = inflater.inflate (R.layout.pagers_fragment1, container, false); ((TextView) V.findviewbyid (R.id.textview1)). SetText (ee.name+ "= ee.
Name-=age "+ ee.age);
return v; @Override public void onactivitycreated (Bundle savedinstancestate) {System.out.println ("ONACTIVITYCR
eated = ");
Super.onactivitycreated (savedinstancestate);
@Override public void Ondestroyview () {System.out.println ("Ondestroyview =" + position);
Super.ondestroyview ();
} @Override public void OnDestroy () {System.out.println ("OnDestroy =" + position);
Super.ondestroy ();
}
}
}
Copy the past directly to see the effect of the V4 package, the XML layout files themselves casually the whole bar.
When you slide to page 3rd, you can see the 1th page destroys the 4th build, currently 2, 3, 4: