ListFragment的使用
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的使用的更多相关文章
- ListFragment源码 (待分析)
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...
- ListFragment创建及其生命周期
相同的ListFragment 带有一个无参构造 一个有参构造 在该Fragment所依附的Activity视图创建时被实例化一次 方法周期分别为1.无参构造2.onInflate3.onAttach ...
- Fragment中添加ListView而不使用ListFragment
最初的构想是,将Fragment和ViewPager结合起来, 然后突发奇想,在第一个Fragment里添加了ListView, 依照网上的建议,extends了ListFragment,接着各种报错 ...
- [Android]ListFragment.setEmptyText() 抛 java.lang.IllegalStateException
在ListFragment子类中直接调用setEmptyText(getString(R.string.msg_no_invited_parties)), 抛java.lang.IllegalStat ...
- Android系列之Fragment(四)----ListFragment的使用
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- ListFragment
ListFragment http://developer.android.com/reference/android/app/ListFragment.html extends Fragment C ...
- Android App组件之ListFragment -- 说明和示例
Android App组件之ListFragment -- 说明和示例 1 ListFragement介绍 ListFragment继承于Fragment.因此它具有Fragment的特性,能够作为a ...
- Android ListFragment实例Demo(自己定义适配器)
上一篇文章介绍了ListFragment,当中的ListView并没有自己定义适配器,实际上在实际开发中常会用到自己定义适配器,是实现更复杂的列表数据展示. 所以这篇文章添加了自己定义适配器.来进行L ...
- android,在fragment中使用listview,不使用listfragment
public class LeftFragment extends Fragment{ private ListView listView; @Override public View onCreat ...
随机推荐
- C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素”
Q: 在反序列化 Xml 字符串为 Xml 对象时,抛出如下异常. 即在 XML文档(0, 0)中有一个错误:缺少根元素. A: 首先看下代码: StringBuilder sb = new Stri ...
- H3 BPM社区:流程开发者的学习交流平台
企业上市有上市流程,融资扩充有融资流程,项目招投标有招投标流程,部门领导选拔有晋升流程,员工请假休假有请假流程,早起上班梳洗有符合自己习惯的流程--生活处处是流程,流程无处不在.但从信息化建设来说,企 ...
- nexus 社区版3.0.2部署、访问
下载nexus社区办(oss): https://www.sonatype.com/download-oss-sonatype 目前最新版本 nexus-3.0.2-02-win64.zip nex ...
- 解决托管在Windows上的Stash的Pull request无法合并的问题
最近尝试合并一个托管在Windows的Stash系统中的pull request时,发现合并按钮被禁用,显示有冲突不能合并,但是在diff页面中没有现实冲突,而且代码实际上并没有任何冲突. 后来在这篇 ...
- YY一下十年后的自己
ps:其实这篇文章的评论比文章本身更有意思,欢迎关注. 每到年底总是我最焦虑的时候,年龄越大情况越明显. 可能越长大越是对 时光的流逝 更有感触,有感触之后就会胡思乱想.所以随手开始写下这篇文章. 人 ...
- CSS 实现打字效果
JS实现 最近做项目的时候需要实现一个字符逐个出现的打字效果,在网上一搜有个不错的jQuery插件Typed.js,效果很赞 <div class="element"> ...
- 苹果台式一体机笔记本安装win双系统攻略教程
步骤 序:win系统下载 :http://www.itellyou.cn 选择要安装的系统进行下载,本文以win7为例 进入苹果系统,左上角——前往——实用工具——BootCamp 助理 点击继续 ...
- JSON.stringify()与JSON.parse()
JSON.stringify()用于把一个对象解析成字符串,如 var student = { age: 23, name: 'wang' } JSON.stringify(student); 结果: ...
- Asp.net中存储过程拖拽至dbml文件中,提示无法获得返回值
Asp.net中存储过程拖拽至dbml文件中,提示无法获得返回值,去属性表中设置这时候会提示你去属性表中更改返回类型. 其实存储过程返回的也是一张表,只不过有时候存储过程有点复杂或者写法不规范的话不能 ...
- JS作用域面试题总结
关于JS作用域问题,是面试的时候面试官乐此不疲的面试题,有时候确实是令人抓狂,今天看到一个讲解这个问题的视频,明白了那些所谓的“原理”顿时有种豁然开朗的感觉~~~ 1.js作用域(全局变量,局部变量) ...