转载请注明:http://www.cnblogs.com/igoslly/p/6959108.html

ListFragment

ListFragment是继承于Fragment的类,专门用于包含ListView的布局文件设置。

当然如果你不想了解ListFragment,通过使用普通Fragment进行setAdapter设置亦是可以的,普通ListView设置参见前章:http://www.cnblogs.com/igoslly/p/6947225.html

配置ListFragment通常涉及3个Layout文件:

1、包含Fragment的主Activity Layout:activity_main.xml  (可直接静态添加fragment,或设置framelayout动态添加)

2、应用ListFragment的 Layout:history_list.xml

ListFragment的布局默认包含一个listVew,命名为:“@id/android:id” (和普通命名语法不同)

还可另设 TextView 用于无数据时显示,命名为:“@id/android:empty”

3、布局中ListView每个item的设置Layout:history_list_competition.xml

以下我实际应用所写的实例,使用的是动态添加fragment,自定义BaseAdapter的方法。

—— ArrayAdapter & SimpleAdapter的设置更为简单,可参考前章

—— 静态添加fragment的方法,即是一个函数findViewById 和 findViewByTag的区别,也可详见苏白的专栏:http://blog.csdn.net/kakaxi1o1/article/details/29368645

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/history_list"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>

historyFragment.java

在 onCreateView()中,调用 history_list.xml 作为该ListFragment的布局文件。

fragmentTranscation.replace(R.id.history_list, historyListFragment).commit();

动态添加historyListFragment,并替换原有fragment

public class HistoryFragment extends Fragment {
private FragmentManager fragmentManager; public HistoryFragment() {
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_list, container, false);
return rootView;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button competition_selected = (Button) getActivity().findViewById(R.id.history_competition);
competition_selected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentManager = getFragmentManager();
fragmentTranscation = fragmentManager.beginTransaction();
HistoryListFragment historyListFragment = new HistoryListFragment();
fragmentTranscation.replace(R.id.history_list, historyListFragment).commit();
}}
);
}
}

history_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/list_content">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"/> <TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""/>
</LinearLayout>

history_list_competition.xml

设置ListView的每个item的布局格式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Null"
android:textSize="20sp"
android:padding="2dp"
android:textColor="@color/black"
android:id="@+id/list_competition_player"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Null"
android:textSize="16sp"
android:padding="2dp"
android:id="@+id/list_competition_date"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="100"
android:gravity="center"
android:textSize="24sp"
android:textColor="@color/black"
android:id="@+id/list_competition_scoreA"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="—"
android:gravity="center"
android:textSize="28sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="100"
android:gravity="center"
android:textSize="24sp"
android:textColor="@color/black"
android:id="@+id/list_competition_scoreB"/>
</LinearLayout>
</LinearLayout>

HistoryListFragment.java

在 onCreate()中,通过setListAdapter() 设置R.layout.history_list_competition。或者使用系统的默认的R.layout.simple_list_item_1;

添加ListView的点击事件自定义BaseAdapter

注意! 如需使用本Java代码,请另行添加具体List<Map<String,Object>>值,否则会报错。

public class HistoryListFragment extends ListFragment {

    private CompetitionListAdapter adapter;
private List<Map<String,Object>> competitionlist; // 构造函数
public HistoryListFragment(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
competitionlist = new ArrayList<Map<String,Object>>();
adapter = new CompetitionListAdapter(getActivity());
//绑定适配器时,必须通过ListFragment.setListAdapter()接口,而不是ListView.setAdapter()或其它方法
this.setListAdapter(adapter);
} // 创建窗口
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.history_list, container, false);
} // 设置点击事件
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id); HashMap<String, Object> item =(HashMap<String, Object>) adapter.getItem(position);
String scoreA = (String)item.get("scoreA");
String scoreB= (String)item.get("scoreB");
String log = (String)item.get("log");
} // 自定义 CompetitionListAdapter 继承于BaseAdapter
public class CompetitionListAdapter extends BaseAdapter {
private LayoutInflater mInflater=null;
public CompetitionListAdapter(Context context){
this.mInflater=LayoutInflater.from(context);
}
@Override
public int getCount(){
return competitionlist.size();
}
@Override
public Object getItem(int position){
return competitionlist.get(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
if (convertView ==null){
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.history_list_competition,null);
holder.date=(TextView)convertView.findViewById(R.id.list_competition_date);
holder.scoreA=(TextView)convertView.findViewById(R.id.list_competition_scoreA);
holder.scoreB=(TextView) convertView.findViewById(R.id.list_competition_scoreB);
holder.player=(TextView)convertView.findViewById(R.id.list_competition_player);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.date.setText((String)competitionlist.get(position).get("date"));
holder.scoreA.setText((String)competitionlist.get(position).get("scoreA"));
holder.scoreB.setText((String)competitionlist.get(position).get("scoreB"));
holder.player.setText((String)competitionlist.get(position).get("player"));
return convertView;
} private class ViewHolder{
public TextView date;
public TextView player;
public TextView scoreB;
public TextView scoreA;
}
}
}

总体效果图如下:

Android开发笔记(13)——ListFragment的更多相关文章

  1. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  2. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...

  3. [置顶] Android开发笔记(成长轨迹)

    分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...

  4. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  5. Android开发笔记--hello world 和目录结构

    原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...

  6. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

  7. [APP] Android 开发笔记 002-命令行创建默认项目结构说明

    接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.

  8. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  9. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  10. 【转】Android开发笔记——圆角和边框们

    原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...

随机推荐

  1. 网络编程:tcp、udp、socket、struct、socketserver

    一.TCP.UDP 一.ARP(Address Resolution Protocol)即地址解析协议,用于实现从 IP 地址到 MAC 地址的映射,即询问目标IP对应的MAC地址. 二.在网络通信中 ...

  2. 51nod1103 N的倍数

    [题解] 先预处理出模N意义下的前缀和sum[i]. 1.如果sum[i]=0,那么1~i的数之和就是N的倍数 2.sum[i]%N总共有0~N-1这N种情况:根据1,如果sum[i]为0则必定有解: ...

  3. hdu2000 ASCII码排序【C++】

    ASCII码排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. 【郑轻邀请赛 A】tmk射气球

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2127 [题意] [题解] 把气球和飞艇所代表的直线投影到xoy面上 设气球所在位置为 ...

  5. Git学习总结(13)——使用git.oschina作为自己的源代码在线管理库

    工作有几年了,期间积累了很多的代码片段,一直想找个存放的地方,方便随时的取用.以前可能是放在自己电脑的硬盘中,但毕竟这样使用起来还是有很多不便. 下面通过码云来说明 一下设置过程.其实,码云和GitH ...

  6. ggplot画基本图形类型

    df<-data.frame( x=c(3,1,5), y=c(2,4,6), label=c("a","b","c"))p<- ...

  7. nyoj_88_汉诺塔(一)_201308201730

    汉诺塔(一)时间限制:1000 ms | 内存限制:65535 KB难度:3描述在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵天在创 ...

  8. [bzoj1704][Usaco2007 Mar]Face The Right Way 自动转身机_贪心

    Face The Right Way 自动转身机 bzoj-1704 Usaco-2007 Mar 题目大意:不想描述题意系列++... ...题目链接 注释:略. 想法:我们直接枚举k,然后从左往右 ...

  9. 机器学习1k近邻

    自己一直学习计算机视觉方面的东西,现在想学习一下数据挖掘跟搜索引擎,自己基础也有点薄弱,看朱明的那本数据挖掘,只能片面的了解这个数据挖掘.不过最近有一本书 机器学习实战,于是乎通过实战的形式了解一下基 ...

  10. SQL SEVER 2008中的演示样例数据库

    SQL SEVER 2008数据库是什么我就不说了,我在这里分享一下怎样学习SQL SEVER 2008数据库,假设是对数据库或是SQL SEVER 数据库全然陌生或是不熟悉的人来说,建议看看一些视频 ...