ListView用法解析
来源:互联网 发布:大数据专家 编辑:程序博客网 时间:2024/05/10 11:29
首先还是先把布局文件给大家贴出来
R.layout.frame.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:paddingTop="1pt" android:paddingLeft="1pt" android:paddingRight="10pt" android:orientation="horizontal" ><TextView android:id ="@+id/appname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15pt" /></LinearLayout>
这是activity采用的布局文件,现在解释一下中间几个属性的含义
android:orientation 指定LineatLayout 是横向的还是纵向的 "vertical"纵向
android:drawSelectorOnTop 指定 容器是否画在内容上方
选择"false"
android:scrollbars指定拖动条的方向 当然我选择了数值方向,由于我做的条目不够多显示不出来拖动条
R.layout.frameitem.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:paddingTop="1pt" android:paddingLeft="1pt" android:paddingRight="10pt" android:orientation="horizontal" ><TextView android:id ="@+id/appname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15pt" /></LinearLayout>
这是用来填充上一个布局文件中ListView每一个条目的布局文件这里我只是放置了一个TextView。
android:paddingTop 控件和父控件的上边距大小 pt为长度单位 关于 dpi px pt 等一些长度计量单位大家可以网上去搜这里我就不讲了
android:textSize 内容字体大小
ApiDemoFrame.java
package apidemo.study;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.AdapterView.OnItemClickListener;public class ApiDemosFrame extends Activity {private ListView list;private SimpleAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frame); list =(ListView)findViewById(R.id.framelistview); AdapterInitialize(); list.setAdapter(adapter); list.setOnItemClickListener(new ListItemListener()); } class ListItemListener implements OnItemClickListener {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id){System.out.println(parent.toString());System.out.println(view.toString());System.out.println(position+"");System.out.println(id+"");} } private void AdapterInitialize() { ArrayList<HashMap<String,Object>> listresourse=new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> map1 =new HashMap<String,Object>(); map1.put("AppName","App"); HashMap<String,Object> map2 =new HashMap<String,Object>(); map2.put("AppName","Content"); HashMap<String,Object> map3 =new HashMap<String,Object>(); map3.put("AppName","Graphics"); HashMap<String,Object> map4 =new HashMap<String,Object>(); map4.put("AppName","Media"); HashMap<String,Object> map5 =new HashMap<String,Object>(); map5.put("AppName","OS"); HashMap<String,Object> map6 =new HashMap<String,Object>(); map6.put("AppName","Text"); HashMap<String,Object> map7 =new HashMap<String,Object>(); map7.put("AppName","Views"); listresourse.add(map1); listresourse.add(map2); listresourse.add(map3); listresourse.add(map4); listresourse.add(map5); listresourse.add(map6); listresourse.add(map7); adapter=new SimpleAdapter(this,listresourse,R.layout.frameitem,new String[]{"AppName"},new int[]{R.id.appname}); }}
这是activity具体实现方法。
AdapteInitialize方法是实例化一个 SimpleAdapter ,该类是用来给ListView中的每一个Item提供数据 。
SimpleAdapter构造函数中参数含义
this 上下文环境
listresourse 提供数据的 Arrylist
R.layout.frameitem 定义每一个Item的布局方式
new String[]{"AppName"} 指定数据具体来源
new int[]{R.id.appname} 上一个参数指定的数据具体放置的位置 R.id.appname 是R.layout.frameitem中TextView的id
用 list的 setOnItemClickListener方法为每一个Item设置点击事件。
这里用到了内部类
我这里采用实现这个OnItemClickListener接口
其中onItemClick方法中是具体写点击后的响应操作 。
yp 2011.11.9
- ListView用法解析
- Android中ListView用法解析
- ListView 解析
- ListView解析
- ListView解析
- listView用法
- listview用法
- ListView 用法
- ListView用法
- Listview用法
- listview用法
- ListView用法
- listView用法
- listview用法
- ListView用法
- ListView用法
- ListView用法
- ListView用法
- 翻译:Libevent参考手册第一章:设置libevent (2011-03-30 07:56:42)
- 日记
- 2011 ACM/ICPC 大连赛区现场赛题解:Compress the String
- 翻译:libevent参考手册第二章:创建event_base
- 翻译:Libevent参考手册第三章:与事件循环一起工作
- ListView用法解析
- 翻译:libevent参考手册第四章:与事件一起工作
- [翻译]libevent参考手册第五章:辅助类型和函数
- [翻译]libevent参考手册第六章:bufferevent:概念和入门 (2011-04-30 19:26:17)
- 电源管理的Kernel层:
- 两个巨大正整数相加
- Libevent参考手册:Bufferevent:高级话题 (2011-07-17 22:22:20)
- 云计算环境下的 JVM 虚拟化特性初探
- Microsoft SQL Server 2005 数据类型