ListFragment继承了Fragment,顾名思义,ListFragment是一种特殊的Fragment,它包含了一个ListView,在ListView里面显示数据。

1. MainActivity

Java类文件:

package com.example.hzhi.fragmentdemo;

import android.app.Activity;
import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction; public class MainActivity extends Activity {
private FragmentManager manager;
private FragmentTransaction transaction; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); manager = getFragmentManager();
transaction = manager.beginTransaction();
ListFragmentImpl frgImpl = new ListFragmentImpl();
ListFragmentSelf frgSelf = new ListFragmentSelf();
transaction.add(R.id.fragment1, frgImpl, "frgImpl");
transaction.add(R.id.fragment2, frgSelf, "frgSelf");
transaction.commit();
} }

xml布局文件:

<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" > <LinearLayout
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/> <LinearLayout
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/> </LinearLayout>

可见MainActivity是比较简单的,在布局里面放了左右两个ListFragment。

2. ListFragment

2.1 左边的ListFragment

Java类文件:

package com.example.hzhi.fragmentdemo;

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[] classes = {
"计算机网络",
"操作系统",
"C语言",
"Java",
"数据库原理",
"移动开发",
}; @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, classes)); } public void onListItemClick(ListView parent, View v,
int position, long id) {
Log.d(TAG, "onListItemClick");
// 找到ListFragmentSelf
ListFragmentSelf listFragmentSelf = (ListFragmentSelf) getFragmentManager().
findFragmentByTag("frgSelf");
listFragmentSelf.flushData(position); } }

布局文件:

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

2.2 右边的ListFragment

Java类文件:

package com.example.hzhi.fragmentdemo;

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.util.Log;
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; final String[] from = new String[] {"name", "title", "info", "picture"};
final int[] to = new int[] {R.id.text0, R.id.text1, R.id.text2, R.id.picture};
private String[] tname = new String[]{"计算机网络", "操作系统", "C语言", "Java", "数据库原理", "移动开发"};
private String[] ttitle = new String[]{"张三", "李四", "王五", "Tom", "Mike", "Peter"};
private String[] ttime = new String[]{"160", "50", "40", "200", "180", "150"};
private int[] pic = new int[]{R.drawable.jsjwl, R.drawable.czxt, R.drawable.cyy,
R.drawable.java, R.drawable.sjkyl, R.drawable.ydkf}; @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) { Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
flushData(0); } public void onListItemClick(ListView parent, View v,
int position, long id) {
Log.d(TAG, "onListItemClick"); } public void flushData(int p){
// 建立SimpleAdapter,将from和to对应起来
SimpleAdapter adapter = new SimpleAdapter(
this.getActivity(), getSimpleData(p),
R.layout.list_item, from, to);
this.setListAdapter(adapter);
} private List<Map<String, Object>> getSimpleData(int p) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "课程名称");
map.put("info", tname[p]);
list.add(map); map = new HashMap<String, Object>();
map.put("title", "教师姓名");
map.put("info", ttitle[p]);
list.add(map); map = new HashMap<String, Object>();
map.put("title", "学时");
map.put("info", ttime[p]);
list.add(map); map = new HashMap<String, Object>();
map.put("title", "图片");
map.put("picture", pic[p]);
list.add(map); return list;
}
}

布局文件:

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

行布局文件:

<?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/text0"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <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"/> <ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" /> </LinearLayout>

最重要的方法是,当点击左边ListFragment的某一行时,取得改行的position,然后根据Tag找到右边的ListFragment,并调用flushData()方法,使右边的ListFragment刷新数据。

3. 运行结果

ListFragment的使用的更多相关文章

  1. ListFragment源码 (待分析)

    /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  2. ListFragment创建及其生命周期

    相同的ListFragment 带有一个无参构造 一个有参构造 在该Fragment所依附的Activity视图创建时被实例化一次 方法周期分别为1.无参构造2.onInflate3.onAttach ...

  3. Fragment中添加ListView而不使用ListFragment

    最初的构想是,将Fragment和ViewPager结合起来, 然后突发奇想,在第一个Fragment里添加了ListView, 依照网上的建议,extends了ListFragment,接着各种报错 ...

  4. [Android]ListFragment.setEmptyText() 抛 java.lang.IllegalStateException

    在ListFragment子类中直接调用setEmptyText(getString(R.string.msg_no_invited_parties)), 抛java.lang.IllegalStat ...

  5. Android系列之Fragment(四)----ListFragment的使用

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  6. ListFragment

    ListFragment http://developer.android.com/reference/android/app/ListFragment.html extends Fragment C ...

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

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

  8. Android ListFragment实例Demo(自己定义适配器)

    上一篇文章介绍了ListFragment,当中的ListView并没有自己定义适配器,实际上在实际开发中常会用到自己定义适配器,是实现更复杂的列表数据展示. 所以这篇文章添加了自己定义适配器.来进行L ...

  9. android,在fragment中使用listview,不使用listfragment

    public class LeftFragment extends Fragment{ private ListView listView; @Override public View onCreat ...

随机推荐

  1. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  2. MyBatis基础入门--知识点总结

    对原生态jdbc程序的问题总结 下面是一个传统的jdbc连接oracle数据库的标准代码: public static void main(String[] args) throws Exceptio ...

  3. .Net Core上用于代替System.Drawing的类库

    目前.Net Core上没有System.Drawing这个类库,想要在.Net Core上处理图片得另辟蹊径. 微软给出了将来取代System.Drawing的方案,偏向于使用一个单独的服务端进行各 ...

  4. 免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)

    在生活中有一种东西几乎已经快要成为我们的另一个电子”身份证“,那就是二维码.无论是在软件开发的过程中,还是在普通用户的日常中,几乎都离不开二维码.二维码 (dimensional barcode) , ...

  5. bcp 命令实例

    set sql_flow="select Id,',',ApplierName,',',FlowStatus,',',IsApproved,',',CreateTime from *** w ...

  6. SpringMVC视图解析器

    SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视 图解析器.当我们对SpringMVC控制的资源发起 ...

  7. MySQL常见面试题

    1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 键: 在关系中 ...

  8. 跟着老男孩教育学Python开发【第二篇】:Python基本数据类型

    运算符 设定:a=10,b=20 . 算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**3 ...

  9. cmd窗口编码设置

    问题描述:不知道误操作了什么,导致cmd窗口的鼠标显示位置出现错位,如下: 现在要将鼠标位置调整回来. 使用工具:cmd. 操作步骤: 1.查看cmd属性可以看到 可以看到是UTF-8编码格式的,我们 ...

  10. Windows cmd 长时间不输出新内容 直到按下ctrl + c 取消或者回车的解决办法

    换了一台新电脑, 在使用 ant 拷贝大量文件的时候 cmd 窗口过了很久没有继续输出新的内容,远远超过平时的耗时, 以为已经卡死 按下 ctrl + c 取消, 这时并没有取消, 而是输出了新内容, ...