【Android 开发教程】ListFragment
来源:互联网 发布:爱拍猴哥淘宝店 编辑:程序博客网 时间:2024/05/21 09:12
本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/ListFramgent就是一个包含ListView的Fragment,它可以通过数据源(数组或游标)显示一系列的信息。ListFragment是非常有用处的,就像RSS,可能左边显示一个列表,右边显示被选中的列表所对应的内容。
可以通过继承ListFragment创建一个ListFragment对象。下面将展示如何使用ListFragment。
1. 创建一个工程:ListFragmentExample。
2. main.xml中的代码。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" ><fragment android:name="net.manoel.ListFragmentExample.Fragment1" android:id="@+id/fragment1" android:layout_weight="0.5" android:layout_width="0dp" android:layout_height="200dp" /><fragment android:name="net.manoel.ListFragmentExample.Fragment1" android:id="@+id/fragment2" android:layout_weight="0.5" android:layout_width="0dp" android:layout_height="300dp" /></LinearLayout>3、在re/layout下面,新建一个文件:fragment1.xml。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:drawSelectorOnTop="false"/> </LinearLayout>4、在包路径下面新建一个类:Fragment1.java。
public class Fragment1 extends ListFragment { String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford", "Jimmy Carter", "Ronald Reagan", "George H. W. Bush", "Bill Clinton", "George W. Bush", "Barack Obama" }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, presidents)); } public void onListItemClick(ListView parent, View v, int position, long id) { Toast.makeText(getActivity(), "You have selected " + presidents[position], Toast.LENGTH_SHORT).show(); } }5、按F11在模拟器上调试。会看见有两个信息列表。
6、随便点击一行,就会有一个消息弹出。
由于在main.xml中分别设置了两个ListFragment的android:layout_height属性,所以这两个列表的高度不一样。
- 【Android 开发教程】ListFragment
- Android ListFragment
- android Listfragment
- Android ListFragment
- Android开发之ListFragment结合LoaderManager加载数据
- Android开发系列——listfragment的使用例子
- Android ListFragment实例Demo
- Android ListFragment使用
- Android自定义ListFragment
- android之ListFragment
- Android API之ListFragment
- Android自定义ListFragment 显示
- ListFragment
- ListFragment
- listFragment
- ListFragment
- Android UI开发第十七篇——Android Fragment实例(ListFragment)
- Android App组件之ListFragment
- MYSQL
- R语言学习系列(极大似然法)
- 使用Memcache在PHP中调试方法的介绍及应用
- CSS长度单位 px和pt的区别
- 透彻理解---现如今网络交换技术 --- --分组交换Packet Switch,缩写为PS。
- 【Android 开发教程】ListFragment
- 【BFS】魔鬼之城
- 利用VS2010搭建命令行编译环境
- 截至2012年5月23日19点58分支持CUDA的NVIDIA的GPU列表(Geforce)
- 积沙成塔之VC不规则按钮的创建
- System.Random类在极限运算环境下重复生成随机数的对比实验
- Gentoo安装文档,教程汇总
- 安装sqlserver2008 framework 3.5
- 黑马程序员——异常概叙