Through the recent spare time on the study of fragment, try to write a small demo, will be in the development of the time can often use the fragment knowledge put together, wrote this demo on the Android fragment more in-depth, After the relevant project will be very handy, the following first put on a few pictures after the implementation:
The implementation of the function is very easy, but also the main, up and down each is two fragment, above the fragment is a ListView, when clicked Item, the following fragment display the corresponding text specific information:
The detailed implementation process is as follows:
① Create Projectfragmentexam, folder views such as the following (add the demo of the previous fragmentpreference together):
②main.xml file layout, vertical two x Fragment, with <Fragment> Tag declaration
<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 " tools:context= ". Mainactivity " android:orientation=" vertical " android:background=" #7ecef4 "><fragment android: Name= "Com.example.fragementexam.FragementList" android:id= "@+id/frag_list" android:layout_width= "Fill_ Parent " android:layout_height=" 0DP " android:layout_weight=" 2 "/><fragment android:name=" Com.example.fragementexam.FragementDetails " android:id=" @+id/frag_detail " android:layout_width=" fill _parent " android:layout_height=" 0DP " android:layout_weight=" 1 "/></linearlayout>
③fragmentlist.java code, which inherits the Listfragment, notes the introduction of the layout page using the Inflate method of Inflater in the Oncreateview method:
Package Com.example.fragementexam;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.listfragment;import Android.os.bundle;import Android.util.Log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.ListView Import Android.widget.simpleadapter;public class Fragementlist extends Listfragment {private string[] values = new Strin G[] {"Dwarf", "Human", "Night Elf", "Dwarf", "Delaney", "Werewolf"};p rivate int[] images = new int[] {R.drawable.gnome,r.drawable.human, R.drawa Ble.nightelf,r.drawable.dwarf, R.drawable.draenei,r.drawable.werewolf}; @Overridepublic View Oncreateview ( Layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {return inflater.inflate (R.layout.frag_list, container, false);} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); list<map<string, object>> listItems = new arraylist<map<String, Object>> (), for (int i = 0; i < values.length; i++) {map<string, object> listItem = new Hashmap<s Tring, object> (); Listitem.put ("Values", Values[i]); Listitem.put ("Images", images[i]); Listitems.add (ListItem);} Simpleadapter adapter = new Simpleadapter (getactivity (), Listitems,r.layout.list_item, new string[] {"Values", "Images" },new int[] {r.id.txt_title, r.id.img}); Setlistadapter (adapter);} @Overridepublic void Onlistitemclick (ListView l, View v, int position, long ID) {//String item = (string) getlistadapter ( ). GetItem (position); Fragementdetails Frag = (fragementdetails) Getfragmentmanager (). Findfragmentbyid (R.id.frag_detail); if (Frag! = null && frag.isinlayout ()) {switch (position) {Case 0:frag.settext (getString (R.string.gnome)); Break;case 1: Frag.settext (getString (R.string.human)); Break;case 2:frag.settext (GetString (r.string.nightelf)); Break;case 3: Frag.settext (getString (R.string.dwarf)); Break;case 4:frag.settext (GetString (R.string.draenei)); break; Case 5:frag.settext (GetString (R.string.werewolf)); LOG.I ("PDA", "position =" + position);}}
④fragementdetails.java code, this is relatively simple, there is a way to set TextView content, its layout page is only a TextView
Package Com.example.fragementexam;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.TextView ;p ublic class Fragementdetails extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup Container,bundle savedinstancestate) {//TODO auto-generated method Stubreturn inflater.inflate (R.layout.frag_detail, Container,false);} public void SetText (String item) {TextView txt = (TextView) GetView (). Findviewbyid (R.id.txt_detail); Txt.settext (item);}}
the other part is the array, the definition of string, this demo, although simple, but the Android fragment aspects of the often used to do a review, assuming that they write clearly this word, in the future encounter similar projects should be better to cope with more, well, call it off!
Sample based on the Android fragment function