The previous article written fragmentpageradapter, this article to introduce Fragmentstatepageradapter, then the difference between the two:
Fragmentpageradapter more Viewpager for a small number of interfaces, such as tab. The fragment will be saved in memory, though it has been crossed. The Fragmentstatepageradapter and ListView are a bit similar, save the current interface, and the next interface and the previous interface (if any), save up to 3, and the others will be destroyed.
If you want a more detailed understanding, you can view the official website API, below the official website written by the demo:
Realize:
Source:
Layout file:
Fragment_pager.xml (Viewpager and two buttons are laid out):
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:gravity= "center_horizontal" android:orientation= "vertical" android:p adding= "4dip" > <android.support.v4.view.viewpager android:id= "@+id/pager" Android:layout_width= "Ma Tch_parent "android:layout_height=" 0px "android:layout_weight=" 1 "> </android.support.v4.view.viewp ager> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout_weight= "0" android:gravity= "center" android:measurewithlargestchild= "true" Android:or ientation= "Horizontal" > <button android:id= "@+id/goto_first" android:layout_width= "Wra P_content "android:layout_height=" Wrap_content "android:text=" to Home > </Button> <button andRoid:id= "@+id/goto_last" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text= "Turn to Last" > </Button> </LinearLayout></LinearLayout>
Fragment_pager_list.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:background= "@android:d rawable/gallery_thumb" android:orientation= " Vertical "> <!--the TextView is used to display fragment pages--<textview android:id=" @+id/text "Android:lay Out_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "Center_vertical|center_horizo Ntal "android:text=" @string/hello_world "android:textappearance="? Android:attr/textappearancemedium "/> <!--is not empty to display the ListView, if Blank, TextView (data is empty)--<framelayout android:layout_width= "Match_parent" android:layout_height= "0dip" android:layout_weight= "1" > <listview android:id= "@android: Id/list "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android :d rawselectorontop= "false"/> <textview android:id= "@android: Id/empty" android:layout_width= "Match_parent" an droid:layout_height= "Match_parent" android:text= "Data is empty" android:textappearance= "? Android:attr/textapp Earancemedium "/> </FrameLayout></LinearLayout>
Code files:
Mainactivity:
Package Com.fragmentdemo13_fragmentstatepageradapter;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.support.v4.view.viewpager;import Android.view.View;import Android.view.view.onclicklistener;import android.widget.button;/** * Here we call the SUPPORT.V4 package, so mainactivity inherits the fragmentactivity, not the activity. */public class Mainactivity extends Fragmentactivity {public static final int num_items = 10;private Myadapter madapter;pr Ivate Viewpager mpager;private Button button_first, button_last; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.fragment_pager);/** * Similarly, Since the call is Support.v4 's package, here is Getsupportfragmentmanager (), not Getfragmentmanager (); */madapter = new Myadapter (Getsupportfragmentmanager ()); Mpager = (Viewpager) Findviewbyid (R.id.pager); Mpager.setadapter (madapter);/** * Click Back to Home */button_first = (Button) Findviewbyid (R.id.goto_first); button_ First.setonclicklistener (New Onclicklistener () {PublIC void OnClick (View v) {mpager.setcurrentitem (0);}}); * * Click to return to last */button_last = (button) Findviewbyid (r.id.goto_last); Button_last.setonclicklistener (New Onclicklistener ( {public void OnClick (View v) {mpager.setcurrentitem (num_items-1);}});}}
Myadapter.java:
Package Com.fragmentdemo13_fragmentstatepageradapter;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import android.support.v4.app.fragmentstatepageradapter;/** * This inherits from Fragmentstatepageradapter, which, according to the official API, is a better choice when it comes to using a large list page in a project. * (This version of the pager was more useful when there was a large number of * pages, working more like a list view.) */pu Blic class Myadapter extends Fragmentstatepageradapter {public myadapter (fragmentmanager FM) {super (FM);} /** * Only need to implement the following two methods. */@Overridepublic Fragment getItem (int position) {return arraylistfragment.newinstance (position);} @Overridepublic int GetCount () {return mainactivity.num_items;}}
Arraylistfragment.java:
Package Com.fragmentdemo13_fragmentstatepageradapter;import Java.util.arraylist;import Android.os.Bundle;import Android.support.v4.app.listfragment;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.textview;import Android.widget.toast;public class Arraylistfragment extends listfragment {private int Mnum;public arraylist<string> list = new arraylist<string> ();/** * Creates an instance of the calculated fragment page, with rage num as a parameter. * (Create a new instance of * countingfragment, providing "num" as an argument.) */public static arraylistfragment Newinst ance (int num) {arraylistfragment f = new arraylistfragment (); Bundle args = new Bundle (), Args.putint ("num", num), f.setarguments (args); return f;} /** * When the method is called, retrieves the number of arguments for this instance. * (When creating, retrieve this instance's number from the * its arguments.) */@Overridepublic void onCreate (Bundle savedinsta Ncestate) {super.oncreate (savedinstancestate);Mnum = Getarguments ()! = null? Getarguments (). GETINT ("num"): 1;} /** * Fragment UI only displays the page number where it is located. * (The Fragment ' s UI is just a simple text view * showing its instance number.) */@Overridepublic view Oncreateview (Layout Inflater inflater, ViewGroup container,bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_ Pager_list, Container,false); TextView TV = (TextView) View.findviewbyid (R.id.text); Tv.settext ("Fragment #" + mnum); return view;} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); Setlistadapter (New arrayadapter<string> (Getactivity (), Android. R.layout.simple_list_item_1, GetData ()));} /** * The data displayed in each fragment list. */private arraylist<string> GetData () {for (int i = 0; i <; i++) {List.add ("Nihao" + i);} return list;} @Overridepublic void Onlistitemclick (ListView l, View v, int position, long id) {Toast.maketext (getactivity (), "you clicked" +pos ition, 0). Show ();}}
Source code Download:
Click to download the source code