欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android App中使用ListFragment的实例教程

程序员文章站 2024-03-02 17:22:34
listfragment继承于fragment。因此它具有fragment的特性,能够作为activity中的一部分,目的也是为了使页面设计更加灵活。 相比fragmen...

listfragment继承于fragment。因此它具有fragment的特性,能够作为activity中的一部分,目的也是为了使页面设计更加灵活。
相比fragment,listfragment的内容是以列表(list)的形式显示的。listfragment的布局默认包含一个listview。因此,在listfragment对应的布局文件中,必须指定一个 android:id 为 “@android:id/list” 的listview控件!

listfragment基础使用
下面介绍在activity中显示listfragment的步骤。

1. activity对应的代码

public class fragmenttest extends activity {
 @override
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.main);
 } 
}

2. 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>

说明:该activity的布局中只包行了一个fragment。下面看看mylistfragment的内容。

3. 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);
  // 设置listfragment默认的listview,即@id/android:list
  this.setlistadapter(new arrayadapter<string>(getactivity(), 
    android.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();
 } 
}

说明:mylistfragment是自定义的listfragment。它使用了list_fragment.xml作为布局,并通过android.r.layout.simple_list_item_1显示listview中的每一项。

4. 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:orientation="vertical" >

 <!-- listfragment对应的android:id值固定为"@id/android:list" -->
 <listview
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:drawselectorontop="false"
  />

</linearlayout>

"activity的布局以及代码"和前面一样,这里就不再重复说明。

5. 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);
  // 建立simpleadapter,将from和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 arraylist<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;
 }
}

说明:mylistfragment使用了r.layout.list_fragment作为布局,并且对于listview中的每一项都使用了r.layout.item作为布局。

6. list_fragment.xml的内容

<!-- listfragment对应的android:id值固定为"@id/android:list" -->
<listview
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawselectorontop="false"
    />

7. 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:orientation="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实例
应用实例说明:建立一个activity,包括2个listfragment。第1个listfragment采用中listview每一行的内容通过android自带的android.r.layout.simple_list_item_1布局来显示;第2个listfragment每一行的内容通过自定义的layout文件来显示,每一行显示两个文本。

activity对应的layout文件代码:

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

说明:
(01) 该layout布局包含两个fragment。
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);  
 }
}

说明:
(01) 在 oncreateview()中,调用list_fragment_impl作为该listfragment的布局文件。
(02) 在 oncreate()中,通过setlistadapter() 设置android.r.layout.simple_list_item_1为listview每一行的布局文件,设置cities为其中数据的每一项内容。

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);
  // 设置listfragment默认的listview,即@id/android:list
  this.setlistadapter(new arrayadapter<string>(getactivity(), 
    android.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();
 } 
}

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:orientation="vertical" >
 
 <!-- listfragment对应的android:id值固定为"@id/android:list" -->
 <listview
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:drawselectorontop="false"
  />
 
</linearlayout>

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);
  // 建立simpleadapter,将from和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 arraylist<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;
 }
}

说明:

(01) 在 oncreateview()中,调用list_fragment_self作为该listfragment的布局文件。
(02) 在 oncreate()中,通过setlistadapter() 设置r.layout.two_textview为listview每一行的布局文件。


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:orientation="vertical" >
 
 <!-- listfragment对应的android:id值固定为"@id/android:list" -->
 <listview
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:drawselectorontop="false"
  />
 
</linearlayout>

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:orientation="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>

效果图:

Android App中使用ListFragment的实例教程