The implementation of the function is very simple, but also the most basic, up and down is two fragment, above the fragment is a ListView, when clicked Item, the following fragment display the corresponding text details
The specific implementation steps are as follows:
① Create the project Fragmentexam, the catalog view is as follows (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 classFragementlist extends Listfragment {PrivateString[] values =NewString[] {"Midget","Human","Night Elf","Dwarf","Delaney", "Wolf Man" }; Private int[] Images =New int[] {r.drawable.gnome, R.drawable.human, R.drawable.nightelf, r.drawable . Dwarf, R.drawable.draenei, R.drawable.werewolf}; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { returnInflater.inflate (R.layout.frag_list, container,false); } @Override Public voidonactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); List<map<string, object>> listItems =NewArraylist<map<string, object>>(); for(inti =0; i < values.length; i++) {Map<string, object> ListItem =NewHashmap<string, object>(); Listitem.put ("Values", values); Listitem.put ("Images", images); Listitems.add (ListItem); } Simpleadapter Adapter=NewSimpleadapter (Getactivity (), ListItems, R.layout.list_item,NewString[] {"Values","Images" }, New int[] {r.id.txt_title, r.id.img}); Setlistadapter (adapter); } @Override Public voidOnlistitemclick (ListView L, View V,intPositionLongID) {//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)); Break; }} log.i ("PDA","position ="+position); }}
④fragementdetails.java code, this is relatively simple, there is a way to set TextView content, its layout page is just 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 ; Public classFragementdetails extends Fragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { //TODO auto-generated Method Stub returnInflater.inflate (R.layout.frag_detail, container,false); } Public voidSetText (String item) {TextView txt=(TextView) GetView (). Findviewbyid (R.id.txt_detail); Txt.settext (item); } }
The other part is a few arrays, the definition of string, this demo, although simple, but the Android fragment aspects commonly used to do a review, if you write this, you will encounter similar projects in the future should be better to cope with more
Examples of Android fragment features