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

1. MainActivity

Java类文件:

  1. package com.example.hzhi.fragmentdemo;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.app.FragmentManager;
  6. import android.app.FragmentTransaction;
  7.  
  8. public class MainActivity extends Activity {
  9. private FragmentManager manager;
  10. private FragmentTransaction transaction;
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16.  
  17. manager = getFragmentManager();
  18. transaction = manager.beginTransaction();
  19. ListFragmentImpl frgImpl = new ListFragmentImpl();
  20. ListFragmentSelf frgSelf = new ListFragmentSelf();
  21. transaction.add(R.id.fragment1, frgImpl, "frgImpl");
  22. transaction.add(R.id.fragment2, frgSelf, "frgSelf");
  23. transaction.commit();
  24. }
  25.  
  26. }

xml布局文件:

  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. android:orientation="horizontal" >
  6.  
  7. <LinearLayout
  8. android:id="@+id/fragment1"
  9. android:layout_weight="1"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:orientation="vertical"/>
  13.  
  14. <LinearLayout
  15. android:id="@+id/fragment2"
  16. android:layout_weight="1"
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:orientation="vertical"/>
  20.  
  21. </LinearLayout>

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

2. ListFragment

2.1 左边的ListFragment

Java类文件:

  1. package com.example.hzhi.fragmentdemo;
  2.  
  3. import android.app.ListFragment;
  4. import android.widget.ListView;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.ArrayAdapter;
  10. import android.util.Log;
  11. import android.widget.Toast;
  12. import android.widget.SimpleAdapter;
  13.  
  14. import java.util.Map;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.ArrayList;
  18.  
  19. public class ListFragmentImpl extends ListFragment{
  20. private static final String TAG = "ListFragmentImpl";
  21.  
  22. private ListView selfList;
  23.  
  24. String[] classes = {
  25. "计算机网络",
  26. "操作系统",
  27. "C语言",
  28. "Java",
  29. "数据库原理",
  30. "移动开发",
  31. };
  32.  
  33. @Override
  34. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  35. Bundle savedInstanceState) {
  36. Log.d(TAG, "onCreateView");
  37. return inflater.inflate(R.layout.list_fragment_impl, container, false);
  38. }
  39.  
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. Log.d(TAG, "onCreate");
  43. super.onCreate(savedInstanceState);
  44. // 设置ListFragment默认的ListView,即@id/android:list
  45. this.setListAdapter(new ArrayAdapter<String>(getActivity(),
  46. android.R.layout.simple_list_item_1, classes));
  47.  
  48. }
  49.  
  50. public void onListItemClick(ListView parent, View v,
  51. int position, long id) {
  52. Log.d(TAG, "onListItemClick");
  53. // 找到ListFragmentSelf
  54. ListFragmentSelf listFragmentSelf = (ListFragmentSelf) getFragmentManager().
  55. findFragmentByTag("frgSelf");
  56. listFragmentSelf.flushData(position);
  57.  
  58. }
  59.  
  60. }

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <!-- ListFragment对应的android:id值固定为"@id/android:list" -->
  8. <ListView
  9. android:id="@id/android:list"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:drawSelectorOnTop="false"
  13. />
  14.  
  15. </LinearLayout>

2.2 右边的ListFragment

Java类文件:

  1. package com.example.hzhi.fragmentdemo;
  2.  
  3. import android.app.ListFragment;
  4. import android.widget.ListView;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.util.Log;
  10. import android.widget.SimpleAdapter;
  11.  
  12. import java.util.Map;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.ArrayList;
  16.  
  17. public class ListFragmentSelf extends ListFragment{
  18. private static final String TAG = "ListFragmentImpl";
  19.  
  20. private ListView selfList;
  21.  
  22. final String[] from = new String[] {"name", "title", "info", "picture"};
  23. final int[] to = new int[] {R.id.text0, R.id.text1, R.id.text2, R.id.picture};
  24. private String[] tname = new String[]{"计算机网络", "操作系统", "C语言", "Java", "数据库原理", "移动开发"};
  25. private String[] ttitle = new String[]{"张三", "李四", "王五", "Tom", "Mike", "Peter"};
  26. private String[] ttime = new String[]{"160", "50", "40", "200", "180", "150"};
  27. private int[] pic = new int[]{R.drawable.jsjwl, R.drawable.czxt, R.drawable.cyy,
  28. R.drawable.java, R.drawable.sjkyl, R.drawable.ydkf};
  29.  
  30. @Override
  31. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  32. Bundle savedInstanceState) {
  33. Log.d(TAG, "onCreateView");
  34. return inflater.inflate(R.layout.list_fragment_self, container, false);
  35. }
  36.  
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39.  
  40. Log.d(TAG, "onCreate");
  41. super.onCreate(savedInstanceState);
  42. flushData(0);
  43.  
  44. }
  45.  
  46. public void onListItemClick(ListView parent, View v,
  47. int position, long id) {
  48. Log.d(TAG, "onListItemClick");
  49.  
  50. }
  51.  
  52. public void flushData(int p){
  53. // 建立SimpleAdapter,将from和to对应起来
  54. SimpleAdapter adapter = new SimpleAdapter(
  55. this.getActivity(), getSimpleData(p),
  56. R.layout.list_item, from, to);
  57. this.setListAdapter(adapter);
  58. }
  59.  
  60. private List<Map<String, Object>> getSimpleData(int p) {
  61. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  62.  
  63. Map<String, Object> map = new HashMap<String, Object>();
  64. map.put("title", "课程名称");
  65. map.put("info", tname[p]);
  66. list.add(map);
  67.  
  68. map = new HashMap<String, Object>();
  69. map.put("title", "教师姓名");
  70. map.put("info", ttitle[p]);
  71. list.add(map);
  72.  
  73. map = new HashMap<String, Object>();
  74. map.put("title", "学时");
  75. map.put("info", ttime[p]);
  76. list.add(map);
  77.  
  78. map = new HashMap<String, Object>();
  79. map.put("title", "图片");
  80. map.put("picture", pic[p]);
  81. list.add(map);
  82.  
  83. return list;
  84. }
  85. }

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <!-- ListFragment对应的android:id值固定为"@id/android:list" -->
  8. <ListView
  9. android:id="@id/android:list"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:drawSelectorOnTop="false"
  13. />
  14.  
  15. </LinearLayout>

行布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView android:id="@+id/text0"
  8. android:textSize="12sp"
  9. android:textStyle="bold"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"/>
  12.  
  13. <TextView android:id="@+id/text1"
  14. android:textSize="12sp"
  15. android:textStyle="bold"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"/>
  18.  
  19. <TextView android:id="@+id/text2"
  20. android:textSize="24sp"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"/>
  23.  
  24. <ImageView
  25. android:id="@+id/picture"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:paddingLeft="10dp" />
  29.  
  30. </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. .NET同步与异步之相关背景知识(六)

    在之前的五篇随笔中,已经介绍了.NET 类库中实现并行的常见方式及其基本用法,当然.这些基本用法远远不能覆盖所有,也只能作为一个引子出现在这里.以下是前五篇随笔的目录: .NET 同步与异步之封装成T ...

  2. SHA-1算法

    SHA-1.h #ifndef _SHA1_H #define _SHA1_H #include<iostream> using namespace std; //4个函数 #define ...

  3. mysql数据库主从同步

    环境: Mater:   CentOS7.1  5.5.52-MariaDB  192.168.108.133 Slave:   CentOS7.1  5.5.52-MariaDB  192.168. ...

  4. Linux硬件IO的优化简介

    Linux硬件IO的优化简介 首先简单介绍下有哪些硬件设备如下(由于硬件种类厂家等各种因素我就不在此多做介绍有兴趣的可以自行学习): 1.CPU:中央处理器,是计算机运算控制的核心部件之一,相当于人的 ...

  5. windows下的命令行工具babun

    什么是babun babun是windows上的一个第三方shell,在这个shell上面你可以使用几乎所有linux,unix上面的命令,他几乎可以取代windows的shell.用官方的题目说就是 ...

  6. python selenium

    https://segmentfault.com/a/1190000007249396?_ea=1293878

  7. 【原】npm 常用命令详解

    今年上半年在学习gulp的使用,对npm的掌握是必不可少的,经常到npm官网查询文档让我感到不爽,还不如整理了一些常用的命令到自己博客上,于是根据自己的理解简单翻译过来,终于有点输出,想学习npm这块 ...

  8. 解除win7网络限速.

    在电脑刚买或者系统重装了的时候,win7系统会默认限制20%的网络速度,限制了我们的上网速度,我们可以解决这个限制,让上网变得更快 下面是操作步骤 1.开始>运行 2.输入以下命令,然后确定 g ...

  9. Linux.NET学习手记(7)

    前一篇中,我们简单的讲述了下如何在Linux.NET中部署第一个ASP.NET MVC 5.0的程序.而目前微软已经提出OWIN并致力于发展VNext,接下来系列中,我们将会向OWIN方向转战. 早在 ...

  10. 一步步学习javascript基础篇(7):BOM和DOM

    一.什么是BOM.什么是DOM BOM即浏览器对象模型,主要用了访问一些和网页无关的浏览器功能.如:window.location.navigator.screen.history等对象. DOM即文 ...