转自:http://www.cnblogs.com/skywang12345/p/3160260.html

1 ListFragement介绍

ListFragment继承于Fragment。因此它具有Fragment的特性,能够作为activity中的一部分,目的也是为了使页面设计更加灵活。

相比Fragment,ListFragment的内容是以列表(list)的形式显示的。

1.1 ListFragment布局

ListFragment的布局默认包含一个list view。因此,在ListFragment对应的布局文件中,必须指定一个 android:id 为 “@android:id/list” 的ListView控件! 若用户向修改list view的,可以在onCreateView(LayoutInflater, ViewGroup, Bundle)中进行修改。当然,用户也可以在ListFragment的布局中包含其它的控件。

下面是ListFragment对应的一个layout示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"> <ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight=""
android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>

ListView中每一行的显示内容,是通过设置适配器来实现的。我们既可以自定义,也可以采用系统默认的layout。后面的应用实例中,会分别列举2种情况下的显示。

1.2 绑定数据

ListFragment绑定ListView的数据,必须通过ListFragment.setListAdapter()接口来绑定数据,而不是使用ListView.setAdapter() 或其它方法!

2 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=""
android:layout_width="match_parent"
android:layout_height="match_parent" /> <fragment
android:name="com.skywang.app.ListFragmentSelf"
android:id="@+id/fragment2"
android:layout_weight=""
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的代码:

skywang12345
导航 博客园
首页
新随笔
联系
订阅订阅
管理 < 2015年3月 >
日 一 二 三 四 五 六 统计 随笔 -
文章 -
评论 -
引用 - 搜索 常用链接 我的随笔
我的评论
我的参与
最新评论
我的标签 随笔分类() Android()
Android NDK编程()
Android 系统层()
Android 应用层()
Computer Culture()
Java()
Linux/Ubuntu()
UML()
Windows()
设计模式()
数据结构_算法()
索引() 最新评论 . Re:Linux内核中双向链表的经典实现
good
--乾卦
. Re:编辑器之神VIM 总结(一) 基础部分
good
--乾卦
. Re:Java多线程系列--“基础篇”10之 线程优先级和守护线程
本模块使用普通代码的线程规则,不过,结果不能说明一切,优(you)先级产生方法、主函数、概念。这个线程优先级就是java的执行方法。
--主的爱
. Re:数据结构与算法系列 目录
楼主加油!顶一记!
--阿水
. Re:Android之 MTP框架和流程分析
NB.多谢分享
--zhuge1668 阅读排行榜 . Android 布局之GridLayout()
. Android 之窗口小部件详解--App Widget()
. 数据结构与算法系列 目录()
. Java多线程系列目录(共43篇)()
. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例() Android App组件之ListFragment -- 说明和示例 Android App组件之ListFragment -- 说明和示例 ListFragement介绍 ListFragment继承于Fragment。因此它具有Fragment的特性,能够作为activity中的一部分,目的也是为了使页面设计更加灵活。 相比Fragment,ListFragment的内容是以列表(list)的形式显示的。
1.1 ListFragment布局 ListFragment的布局默认包含一个list view。因此,在ListFragment对应的布局文件中,必须指定一个 android:id 为 “@android:id/list” 的ListView控件! 若用户向修改list view的,可以在onCreateView(LayoutInflater, ViewGroup, Bundle)中进行修改。当然,用户也可以在ListFragment的布局中包含其它的控件。 下面是ListFragment对应的一个layout示例:
复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"> <ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight=""
android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout> 复制代码 ListView中每一行的显示内容,是通过设置适配器来实现的。我们既可以自定义,也可以采用系统默认的layout。后面的应用实例中,会分别列举2种情况下的显示。 1.2 绑定数据 ListFragment绑定ListView的数据,必须通过ListFragment.setListAdapter()接口来绑定数据,而不是使用ListView.setAdapter() 或其它方法! 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=""
android:layout_width="match_parent"
android:layout_height="match_parent" /> <fragment
android:name="com.skywang.app.ListFragmentSelf"
android:id="@+id/fragment2"
android:layout_weight=""
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout> 复制代码 说明:
() 该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);
}
} 复制代码 说明: () 在 onCreateView()中,调用list_fragment_impl作为该ListFragment的布局文件。
() 在 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 -- 说明和示例(转载)的更多相关文章

  1. Android App组件之ListFragment -- 说明和示例

    Android App组件之ListFragment -- 说明和示例 1 ListFragement介绍 ListFragment继承于Fragment.因此它具有Fragment的特性,能够作为a ...

  2. Android App组件之Fragment说明和示例

    Android App组件之Fragment说明和示例 1 Fragement介绍 Android从3.0开始引入Fragment,主要是为了支持更动态更灵活的界面设计. Fragment是activ ...

  3. Android App组件之Activity

    Android App组件之Activity 1 activit介绍 Activities 是Android的四大组件之一,其余三大组件是service.broadcast和content provi ...

  4. Android APP架构设计——MVP的使用示例

    0. 前言 为了更好地进行移动端架构设计,我们最常用的就是MVC.MVP和MVVM,作为三个最耳熟能详的三大架构,应用可谓非常广泛.对于这三种架构设计以及优缺点已经在Android APP架构设计-- ...

  5. android.support.v4.app.Fragment和android.app.Fragment区别

    1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support.v ...

  6. [转]android.support.v4.app.Fragment和android.app.Fragment区别

      1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support ...

  7. App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南

    面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本. ...

  8. android四大组件学习总结以及各个组件示例(2)

    上篇博文讲解了activity.content provider,此篇博文来仔细总结service.broadcast receiver: 3. Service >什么是服务?>windo ...

  9. android四大组件学习总结以及各个组件示例(1)

    android四大组件分别为activity.service.content provider.broadcast receiver. 一.android四大组件详解 1.activity (1)一个 ...

随机推荐

  1. 自动化中间人攻击工具subterfuge小实验

    Subterfuge是一款用python写的中间人攻击框架,它集成了一个前端和收集了一些著名的可用于中间人攻击的安全工具. Subterfuge主要调用的是sslstrip,sslstrip 是08 ...

  2. Ghost本地安装highlight.js使代码高亮

    对于程序猿写博客来说,这代码高亮是起码的要求.可是Ghost本身没有支持高亮代码. 可是能够通过扩展来实现,它就是highlight.js--附官方站点,看了下首页介绍,真的非常强大,如今说说怎么进行 ...

  3. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  4. 【LeetCode-面试算法经典-Java实现】【010-Regular Expresssion Matching(正則表達式匹配)】

    [010-Regular Expresssion Matching(正則表達式匹配)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Implement regular ...

  5. android_checkbox_dialog 设计 是不是要开起 默认不提示对话框

    android_checkbox_dialog设计是不是开起默认不提示 package com.example.android_checkbox_dialog; import android.app. ...

  6. css设置背景图片自适应

      CreateTime--2017年12月25日16:36:07 Author:Marydon 控制背景图片100%自适应填充布局 /* 控制背景图片100%自适应填充布局 */ body{ bac ...

  7. C#不用union,而是有更好的方式实现 .net自定义错误页面实现 .net自定义错误页面实现升级篇 .net捕捉全局未处理异常的3种方式 一款很不错的FLASH时种插件 关于c#中委托使用小结 WEB网站常见受攻击方式及解决办法 判断URL是否存在 提升高并发量服务器性能解决思路

    C#不用union,而是有更好的方式实现   用过C/C++的人都知道有个union,特别好用,似乎char数组到short,int,float等的转换无所不能,也确实是能,并且用起来十分方便.那C# ...

  8. 线程池ThreadPoolExecutor分析

    线程池.线程池是什么,说究竟,线程池是处理多线程的一种形式,管理线程的创建,任务的运行,避免了无限创建新的线程带来的资源消耗,可以提高应用的性能.非常多相关操作都是离不开的线程池的,比方android ...

  9. Linux安装Axis C构建WebService服务

    在安装Axis C++之前有两个组件是必须安装的,分别是Apache HTTP Server以及用于处理XML的程序Xerces:为了编译Axis以及Apache HTTPD,你的Linux机器还应该 ...

  10. 基于mac系统的apacheserver的使用流程

    打开终端.输入下面命令:sudo apachectl start 此时Apache已经开启.在浏览器中输入本地ip地址能够看到it works! 打开前往----电脑------Macintosh H ...