转载请注明: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. 优化JAVA查询Mongodb数量过大,查询熟读慢的方法

    前言:2018年的时候优化了一个项目,该项目从MOngodb中获取数据的时候一次去十万百万千万的数据过慢,往往每次都要二十秒,三十秒,今天提出了一个代码优化的方案 项目查从mongodb中获取数据:代 ...

  2. Golang之路

    目录 Golang之路 Golang之路 Golang(一) - 开篇必须吹牛逼 Golang(二) - 第一个go程序和基本语法 Golang(三) - 函数 Golang(四) - 流程控制 Go ...

  3. 3.3.4 lambda 表达式

    lambda表达式常用来声明匿名函数,即没有函数名字的临时使用的小函数,例如第2章中列表对象的sort()方法以及内置函数sorted()中key参数.lambda表达式只可以包含一个表达式,不允许包 ...

  4. TypeError: CleanWebpackPlugin is not a constructor

    在项目中引入clean-webpack-plugin打包后报错 new CleanWebpackPlugin(), ^ TypeError: CleanWebpackPlugin is not a c ...

  5. 赛门铁克通配符SSL证书,一张通配型证书实现全站加密

      赛门铁克通配型SSL证书,验证域名所有权和企业信息,属于企业验证(OV) 级SSL证书,最高支持256位加密.申请通配符SSL证书可以保护相同主域名下无限数量的多个子域名(主机).例如,一个通配符 ...

  6. Servlet请求参数编码处理(POST & GET)

    小巧,但在中文语境下,还是要注意的. 以下是关键语句,注意转码的先后顺序,这源于GET是HTTP服务器处理,而POST是WEB容器处理: String name = request.getParame ...

  7. 贪心算法 Heidi and Library (easy)

    A. Heidi and Library (easy) time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. JAVA大数据项目+整理的Mysql数据库32条军规

    http://www.jianshu.com/users/a9b2d43bb94e/latest_articles

  9. MVC.Net:压缩/保存图片缩略图

    通常用户上传的图片需要压缩或者生成缩略图.用System.Web.Helpers.WebImage的Resize方法可以很方便的实现这一功能.示例代码如下: /// <summary> / ...

  10. iOS 代码方式设置按钮标题、图片的偏移

    button.imageEdgeInsets = UIEdgeInsetsMake(0,1 , 2, 3); button.titleEdgeInsets = UIEdgeInsetsMake(0,1 ...