"Go" example based on the Android fragment feature

Source: Internet
Author: User

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
  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=". Mainactivity "
  6. android:orientation="vertical"
  7. android:background="#7ecef4">
  8. <fragment
  9. android:name="com.example.fragementexam.FragementList"
  10. android:id="@+id/frag_list"
  11. android:layout_width="fill_parent"
  12. android:layout_height="0DP"
  13. android:layout_weight="2"/>
  14. <fragment
  15. android:name="Com.example.fragementexam.FragementDetails"
  16. android:id="@+id/frag_detail"
  17. android:layout_width="fill_parent"
  18. android:layout_height="0DP"
  19. android:layout_weight="1"/>
  20. </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
  1. Package com.example.fragementexam;
  2. Import java.util.ArrayList;
  3. Import Java.util.HashMap;
  4. Import java.util.List;
  5. Import Java.util.Map;
  6. Import android.app.ListFragment;
  7. Import Android.os.Bundle;
  8. Import Android.util.Log;
  9. Import Android.view.LayoutInflater;
  10. Import Android.view.View;
  11. Import Android.view.ViewGroup;
  12. Import Android.widget.ListView;
  13. Import Android.widget.SimpleAdapter;
  14. Public class Fragementlist extends Listfragment {
  15. private string[] values = new string[] { "dwarf", "human", "Night Elf", "dwarf", "Delaney",
  16. "Werewolf"};
  17. private int[] images = new int[] {r.drawable.gnome,
  18. R.drawable.human, R.drawable.nightelf,
  19. R.drawable.dwarf, R.drawable.draenei,
  20. R.drawable.werewolf};
  21. @Override
  22. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
  23. Bundle savedinstancestate) {
  24. return Inflater.inflate (R.layout.frag_list, container, false);
  25. }
  26. @Override
  27. public void onactivitycreated (Bundle savedinstancestate) {
  28. super.onactivitycreated (savedinstancestate);
  29. list<map<string, object>> listItems = new arraylist<map<string, object>> ();
  30. For (int i = 0; i < values.length; i++) {
  31. map<string, object> listItem = new hashmap<string, object> ();
  32. Listitem.put ("values", values[i]);
  33. Listitem.put ("Images", images[i]);
  34. Listitems.add (ListItem);
  35. }
  36. Simpleadapter adapter = New Simpleadapter (Getactivity (), ListItems,
  37. R.layout.list_item, new string[] { "values", "Images"},
  38. new int[] {r.id.txt_title, r.id.img});
  39. Setlistadapter (adapter);
  40. }
  41. @Override
  42. public void Onlistitemclick (ListView l, View v, int position, long id) {
  43. //String item = (string) getlistadapter (). GetItem (position);
  44. Fragementdetails Frag = (fragementdetails) Getfragmentmanager ()
  45. . Findfragmentbyid (R.id.frag_detail);
  46. if (frag! = null && frag.isinlayout ()) {
  47. switch (position) {
  48. Case 0:
  49. Frag.settext (getString (r.string.gnome));
  50. Break ;
  51. case 1:
  52. Frag.settext (getString (R.string.human));
  53. Break ;
  54. Case 2:
  55. Frag.settext (getString (r.string.nightelf));
  56. Break ;
  57. Case 3:
  58. Frag.settext (getString (R.string.dwarf));
  59. Break ;
  60. Case 4:
  61. Frag.settext (getString (R.string.draenei));
  62. Break ;
  63. Case 5:
  64. Frag.settext (getString (R.string.werewolf));
  65. Break ;
  66. }
  67. }
  68. LOG.I ("PDA", "position =" + position);
  69. }
  70. }


④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
  1. Package com.example.fragementexam;
  2. Import android.app.Fragment;
  3. Import Android.os.Bundle;
  4. Import Android.view.LayoutInflater;
  5. Import Android.view.View;
  6. Import Android.view.ViewGroup;
  7. Import Android.widget.TextView;
  8. Public class Fragementdetails extends Fragment {
  9. @Override
  10. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
  11. Bundle savedinstancestate) {
  12. //TODO auto-generated method stub
  13. return Inflater.inflate (R.layout.frag_detail, container,false);
  14. }
  15. public void SetText (String item) {
  16. TextView txt = (TextView) GetView (). Findviewbyid (R.id.txt_detail);
  17. Txt.settext (item);
  18. }
  19. }


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.