An example tutorial on using Listfragment in Android apps _android

Source: Internet
Author: User

Listfragment inherited from fragment. Therefore, it has fragment characteristics and can be used as a part of the activity in order to make the page design more flexible.
The content of the fragment,listfragment is shown in the form of lists (list). The Listfragment layout defaults to include a ListView. Therefore, in the listfragment corresponding layout file, you must specify a ListView control Android:id as "@android: Id/list"!

Listfragment Basic Use
The steps to display listfragment in an activity are described below.

1. The code corresponding to the activity

public class Fragmenttest extends activity {
 @Override public
 void OnCreate (Bundle savedinstancestate) {
  Super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
 } 

2. The layout corresponding to the activity

<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" >

 <fragment 
  android:name= " Com.skw.fragmenttest.MyListFragment "
  android:id=" @+id/myfragment "
  android:layout_width=" Match_parent "
  android:layout_height= "match_parent"/>

</LinearLayout>

Description: Only one fragment is wrapped in the layout of the activity. Let's take a look at the contents of Mylistfragment.

3. Content of Mylistfragment

public class Mylistfragment extends Listfragment {private static final String TAG = "# #MyListFragment # #";

 Private ListView selflist;
   string[] Cities = {"Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan", "Tianjing", "Changsha",

 "Xi ' an", "Chongqing", "Guilin",}; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {LOG.D
  (TAG, "Oncreateview");
 Return Inflater.inflate (R.layout.list_fragment, container, false);
  @Override public void OnCreate (Bundle savedinstancestate) {log.d (TAG, "onCreate");
  Super.oncreate (savedinstancestate); Set the default ListView for Listfragment, that is, @id/android:list This.setlistadapter (New arrayadapter<string> (getactivity), a Ndroid.

 R.layout.simple_list_item_1, cities));
  public void Onlistitemclick (ListView parent, View v, int position, long id) {LOG.D (TAG, "Onlistitemclick"); Toast.maketext (Getactivity (), "You have selected" + CIties[position], Toast.length_short). Show ();

 } 
}

Description: Mylistfragment is a custom listfragment. It uses List_fragment.xml as the layout and displays each item in the ListView by Android.r.layout.simple_list_item_1.

4. Content of List_fragment.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent "
 android:o" rientation= "Vertical" >

 <!--listfragment the corresponding Android:id value is fixed to "@id/android:list"
 -->
  android:id= "@id/android:list"
  android:layout_width= "match_parent"
  android:layout_height= "Match_ Parent " 
  android:drawselectorontop=" false "
  />

</LinearLayout>

"The layout and code of the activity", as before, is no longer repeated here.

5. Content of Mylistfragment

public class Mylistfragment extends Listfragment {private static final String TAG = "# #MyListFragment # #";

 Private ListView selflist; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {LOG.D
  (TAG, "Oncreateview");
 Return Inflater.inflate (R.layout.list_fragment, container, false);
  @Override public void OnCreate (Bundle savedinstancestate) {final string[] from = new string[] {"title", "Info"};

  Final int[] to = new int[] {r.id.text1, r.id.text2};
  LOG.D (TAG, "onCreate");
  Super.oncreate (savedinstancestate); 
    Set up Simpleadapter to correspond from and to simpleadapter adapter = new Simpleadapter (this.getactivity (), Getsimpledata (),
  R.layout.item, from, to);
 This.setlistadapter (adapter);
  public void Onlistitemclick (ListView parent, View v, int position, long id) {LOG.D (TAG, "Onlistitemclick"); Toast.maketext (Getactivity (), "You have selected" + position, Toast.length_short). Show ();
 Private list<map<string, object>> Getsimpledata () {list<map<string, object>> List = new A

  Rraylist<map<string, object>> ();
  map<string, object> map = new hashmap<string, object> ();
  Map.put ("title", "Ferris Wheel");
  Map.put ("info", "Suzhou Ferris Wheel");

  List.add (map);
  Map = new hashmap<string, object> ();
  Map.put ("title", "Flower");
  Map.put ("info", "Roser");

  List.add (map);
  Map = new hashmap<string, object> ();
  Map.put ("title", "Disk");
  Map.put ("info", "Song Disk");

  List.add (map);
 return list;

 }
}

Description: Mylistfragment uses r.layout.list_fragment as the layout, and R.layout.item is used as a layout for each item in ListView.

6. Content of List_fragment.xml

<!--listfragment corresponding Android:id value is fixed as "@id/android:list"-->
<listview
Android:id= "@id/android:list"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:drawselectorontop= "false"
/>

7. Content of Item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent "
 android:o" rientation= "vertical" >

 <textview android:id= "@+id/text1"
  android:textsize= "12SP"
  android: Textstyle= "Bold"
  android:layout_width= "match_parent"
  android:layout_height= "wrap_content"/>

 <textview android:id= "@+id/text2"
  android:textsize= "24sp"
  android:layout_width= "Match_parent"
  android:layout_height= "wrap_content"/>

</LinearLayout>

Listfragment Instance
Application Illustration: Establish an activity, including 2 listfragment. The 1th listfragment the contents of each line in ListView with the Android.r.layout.simple_list_item_1 layout of the Android Ribbon. 2nd listfragment The contents of each row are displayed by a custom layout file, displaying two text per line.

Activity corresponds to the layout file code:

<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" >

 <fragment 
  android:name= "Com.skywang.app.ListFragmentImpl"
  android:id= "@+id/fragment1" 
  android:layout_weight= "1"
  android:layout_width= "Match_parent"
  android:layout_height= "Match_parent"/>

 <fragment 
  android:name= " Com.skywang.app.ListFragmentSelf "
  android:id=" @+id/fragment2 " 
  android:layout_weight=" 1 "
  Android : layout_width= "match_parent"
  android:layout_height= "match_parent"/>

</LinearLayout>

Description
(01) The layout layout contains two fragment.
The code for the activity:

Package Com.skywang.app;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.FragmentManager;
Import android.app.FragmentTransaction;
Import Android.view.Menu;

public class Listfragmenttest extends activity {

 @Override
 protected void onCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.list_fragment_test);  
 }


Description
(01) in Oncreateview (), call List_fragment_impl as the listfragment layout file.
(02) in OnCreate (), set up Android via Setlistadapter (). R.layout.simple_list_item_1 is the layout file for each row of ListView, setting cities as each item of the data.

Code for Listfragmentimpl.java:

Package Com.skywang.app;
Import android.app.ListFragment; 
Import Android.widget.ListView;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ArrayAdapter;
Import Android.util.Log;
Import Android.widget.Toast;

Import Android.widget.SimpleAdapter;
Import Java.util.Map;
Import Java.util.HashMap;
Import java.util.List;

Import java.util.ArrayList;
 
 public class Listfragmentimpl extends listfragment{private static final String TAG = "Listfragmentimpl";
 
 Private ListView selflist;
   string[] Cities = {"Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan", "Tianjing", "Changsha",

 "Xi ' an", "Chongqing", "Guilin",}; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {LOG.D
  (TAG, "Oncreateview");
 Return Inflater.inflate (R.layout.list_fragment_impl, container, false); @Override public void onCreate (Bundle savedinstancestate) {log.d (TAG, "onCreate");
  Super.oncreate (savedinstancestate); Set the default ListView for Listfragment, that is, @id/android:list This.setlistadapter (New arrayadapter<string> (getactivity), a Ndroid.
  
 R.layout.simple_list_item_1, cities));
  public void Onlistitemclick (ListView parent, View v, int position, long id) {LOG.D (TAG, "Onlistitemclick");
 Toast.maketext (Getactivity (), "You have selected" + cities[position], toast.length_short). Show ();

 } 
}

Content of List_fragment_impl.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent "
 android:o" rientation= "Vertical" >
 
 <!--listfragment the corresponding Android:id value is fixed to "@id/android:list"
 -->
  android:id= "@id/android:list"
  android:layout_width= "match_parent"
  android:layout_height= "Match_ Parent " 
  android:drawselectorontop=" false "
  />
 
</LinearLayout>

Code for Listfragmentself.java:

Package Com.skywang.app;

Import android.app.ListFragment; 
Import Android.widget.ListView;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ArrayAdapter;
Import Android.util.Log;
Import Android.widget.Toast;

Import Android.widget.SimpleAdapter;
Import Java.util.Map;
Import Java.util.HashMap;
Import java.util.List;

Import java.util.ArrayList;
 
 public class Listfragmentself extends listfragment{private static final String TAG = "Listfragmentimpl";
 
 Private ListView selflist; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {LOG.D
  (TAG, "Oncreateview");
 Return Inflater.inflate (R.layout.list_fragment_self, container, false); 
  @Override public void OnCreate (Bundle savedinstancestate) {final string[] from = new string[] {"title", "Info"};
  
  Final int[] to = new int[] {r.id.text1, r.id.text2};
  LOG.D (TAG, "onCreate"); Super.oncreate (SAVedinstancestate); 
    Set up Simpleadapter to correspond from and to simpleadapter adapter = new Simpleadapter (this.getactivity (), Getsimpledata (),
  R.layout.two_textview, from, to);
 This.setlistadapter (adapter);
  public void Onlistitemclick (ListView parent, View v, int position, long id) {LOG.D (TAG, "Onlistitemclick");
 Toast.maketext (Getactivity (), "You have selected" + position, Toast.length_short). Show (); Private list<map<string, object>> Getsimpledata () {list<map<string, object>> List = new Ar
  
  Raylist<map<string, object>> ();
  map<string, object> map = new hashmap<string, object> ();
  Map.put ("title", "Ferris Wheel");
  Map.put ("info", "Suzhou Ferris Wheel");

  List.add (map);
  Map = new hashmap<string, object> ();
  Map.put ("title", "Flower");
  Map.put ("info", "Roser");

  List.add (map);
  Map = new hashmap<string, object> ();
  Map.put ("title", "Disk");
  Map.put ("info", "Song Disk"); LiSt.add (map);
 return list;

 }
}

Description

(01) in Oncreateview (), call list_fragment_self as the listfragment layout file.
(02) in OnCreate (), Setlistadapter () sets R.layout.two_textview as the layout file for each row of ListView.


Content of List_fragment_self.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent "
 android:o" rientation= "Vertical" >
 
 <!--listfragment the corresponding Android:id value is fixed to "@id/android:list"
 -->
  android:id= "@id/android:list"
  android:layout_width= "match_parent"
  android:layout_height= "Match_ Parent " 
  android:drawselectorontop=" false "
  />
 
</LinearLayout>

Content of Two_textview.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent "
 android:o" rientation= "vertical" >
 
 <textview android:id= "@+id/text1"
  android:textsize= "12SP"
  android: Textstyle= "Bold"
  android:layout_width= "match_parent"
  android:layout_height= "wrap_content"/>

 <textview android:id= "@+id/text2"
  android:textsize= "24sp"
  android:layout_width= "Match_parent"
  android:layout_height= "wrap_content"/>
  
</LinearLayout>

Effect Chart:

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.