Original URL: http://blog.csdn.net/eyu8874521/article/details/8252216
Through the recent idle time on the study of fragment, try to write a small demo, will be used in the development of fragment knowledge to put together, wrote this demo to the Android fragment more in-depth understanding of the project will be a lot easier to do in the future , let's put a few pictures after the implementation:
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
[HTML]View Plaincopy
- <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:
[Java]View Plaincopy
- 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 string[] { "dwarf", "human", "Night Elf", "dwarf", "Delaney",
- "Werewolf"};
- private int[] images = new int[] {r.drawable.gnome,
- R.drawable.human, R.drawable.nightelf,
- R.drawable.dwarf, R.drawable.draenei,
- R.drawable.werewolf};
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- return Inflater.inflate (R.layout.frag_list, container, false);
- }
- @Override
- public 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<string, 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);
- }
- @Override
- public 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));
- 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
[Java]View Plaincopy
- 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 class Fragementdetails extends Fragment {
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- //TODO auto-generated method stub
- return 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 some arrays, the definition of string, this demo, although simple, but the Android fragment aspects commonly used to do a review, if you write this word, in the future encounter similar projects should be better to cope with more, good, call it off!
"Go" example based on the Android fragment feature