This article is an example of a listfragment that understands the usage of the more commonly used listfragment and the data transfer between the fragment by understanding the instance.
Realize:
The mainactivity includes 1 button+2 fragment (right two), click button, the middle list of lists, click on any of the items, the corresponding item value will be passed to the right fragment and displayed.
Source:
Activity_main:
<linearlayout 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 "Android:orien tation= "Horizontal" tools:context= ". Mainactivity "> <linearlayout android:id=" @+id/left "android:layout_width=" 0DP "Android:layo ut_height= "Match_parent" android:layout_weight= "1" android:background= "#CCCCCC" android:orientation= " Vertical "> <button android:id=" @+id/button "android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:text= "Show list"/> </LinearLayout> <linearlayou T android:id= "@+id/center" android:layout_width= "0DP" android:layout_height= "Match_parent" and roid:layout_weight= "2" android:background= "#CCDDFF" android:orientation= "vertical" > </linearlayout ><linearlayout android:id= "@+id/right" android:layout_width= "0DP" android:layout_height= "Match_pare NT "android:layout_weight=" 2 "android:background=" #CCFFDD "android:orientation=" vertical "> < ;/linearlayout></linearlayout>
Articlelistfragment should also have a layout file, where it is convenient to add a ListView directly in the code, but also because the class inherits the listfragment's sake.
Detailfragment contains the layout files:
<linearlayout 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 " android:orientation= "Horizontal" tools:context= ". Mainactivity "> <textview android:id=" @+id/textview " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:textsize= "30SP"/></linearlayout>
Code files:
Mainactivity:
Package Com.fragmentdemo8_listfragment;import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.view;import android.widget.Button;/* * * An example of Listfragment demo */public class Mainactivity extends Activity {private Button button;private Fragmentmanager Mana Ger;private fragmenttransaction transaction; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); manager = Getfragmentmanager (); button = (button) Findviewbyid (R.id.button);/** * Click the button in the activity to add articlelistfragment to the middle of the layout and display the list data. */button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {transaction = Manager.begintransaction (); Articlelistfragment articlelistfragment = new Articlelistfragment (); Transaction.add (R.id.center, Articlelistfragment, "center"); Transaction.commit ();}});}}
Fragment:articlelistfragment in the middle:
Package Com.fragmentdemo8_listfragment;import Java.util.arraylist;import Java.util.list;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.app.listfragment;import Android.os.bundle;import Android.view.view;import Android.widget.arrayadapter;import Android.widget.ListView; Import android.widget.toast;/** *articlelistfragment Inherits Listfragment and displays some list data. */public class Articlelistfragment extends Listfragment {private arrayadapter<string> adapter;private List< string> data;private fragmentmanager manager;private fragmenttransaction transaction; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);d ata = new arraylist<string> (); for (int i = 0; i <; i++) {Data.add ("Rose" + i);} Manager = Getfragmentmanager (); adapter = new Arrayadapter<string> (Getactivity (), Android. R.layout.simple_list_item_1, data); Setlistadapter (adapter);} @Overridepublic void Onlistitemclick (ListView l, View v, int positiOn, long id) {Super.onlistitemclick (L, V, position, id); String str = adapter.getitem (position), transaction = Manager.begintransaction ();D etailfragment detailfragment = new Detailfragment ();/** * Use bundle class storage to pass data */bundle bundle = new Bundle () bundle.putstring ("id", str); Detailfragment.setarguments (bundle); Transaction.replace (R.id.right, detailfragment, "detail"); Transaction.commit (); Toast.maketext (getactivity (), str, toast.length_short). Show ();}}
Fragment:detailfragment on the right:
Package Com.fragmentdemo8_listfragment;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.TextView /** * Gets the data from the list item in Articlelistfragment and then displays it on the fragment. * */public class Detailfragment extends Fragment {private TextView textview;private view view; @Overridepublic View on CreateView (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view = Inflater.inflate ( R.layout.detail, null); TextView = (TextView) View.findviewbyid (R.id.textview); Bundle bundle = Getarguments (); String str = bundle.getstring ("id"); textview.settext (str); return view;}}
Source code Download:
Click Download source