Android中ListView的用法
使用方法1
显示简单的文本
- 在layout文件中像加入普通控件一样在layout文件中引入ListView
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> 数组中的数据要通过适配器来导入ListView,Android有很多的适配器,常用的有ArrayAdapter,它通过泛型指定要适配的数据类型,然后在构造函数中把要适配的数据传入即可。
//data is a String Array
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity_simplelist.this, android.R.layout.simple_list_item_1, data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
使用方法2
显示定制的界面
- 定义子项的构成
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
} - 然后需要为子项写好布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip" /> </LinearLayout>
3, 接下来还要自定义适配器
3.1 自定义适配器要写构造函数,有三个参数分别是上下文,ListView子项布局的id和数据
public FruitAdapter(Context context, int textViewResourceId,
List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
3.2 还需要重写getView方法,这个方法在滚动屏幕的时候被调用
3.2.1 在getView方法中首先调用getItem取得当前子项的实例
Fruit fruit = getItem(position);
3.2.2 然后调用LayoutInflater来为这个子项加载传入的布局
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
3.2.3 接着调用findViewById找到布局中的各个控件的id,然后调用它们的set函数设置图片或文字
fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
如何提升ListView的效率
方法1: 利用缓存的布局 getView()有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,当这个参数不为null时,直接使用而不是重新加载布局。
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
方法2:对控件实例进行缓存 当加载布局时,将控件保存到ViewHolder中;当不需要重新加载布局时,直接从ViewHolder中取出。
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
Android中ListView的用法的更多相关文章
- Android中Listview点击item不变颜色以及设置listselector 无效
Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- android中ListView控件&&onItemClick事件中获取listView传递的数据
http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...
- android中ListView点击和里边按钮点击不能同时生效问题解决
今天遇到一个问题:android中ListView点击和里边button点击不能同时生效问题解决. 原因是: listView 在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得 ...
- android 中uri.parse()用法
android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...
- Android中ListView无法点击
Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502 问题描述: ListView中Item加入 ...
- Android中Selector的用法(改变ListView和Button的默认背景)
Android中的Selector的用法 http://blog.csdn.net/shakespeare001/article/details/7788400#comments Android中的S ...
- Android中 ListView 详解(二)
本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
随机推荐
- Python -- 数据加载、存储与文件格式
标签(空格分隔): Python 读入读出通常可以划分为几个大类:读取文本文件和其他更高效的磁盘存储格式,加载数据库中的数据,利用Web API操作网络资源. 读写文本格式的数据 pandas提供了一 ...
- 微信小程序-图片、录音、音频播放、音乐播放、视屏、文件
图片: wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 注:文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx ...
- background-sizi (转)
http://www.cnblogs.com/greenteaone/archive/2012/08/28/2659878.html (原创作者链接地址 ) Background-Size:[ & ...
- BW系统之间的InfoProvider数据传输:Export DataSource
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- oracle的imp导入时覆盖目标数据库
背景:oracle正式库通过exp命令导出的dmp备份包,现在通过imp命令还原到测试库,测试库上面的表数据全部不要,要用新的. 方法:先删除用户.用户所在表空间,再新建用户和表空间,再imp导入. ...
- Java项目JUnit简单使用
前面自己写了一个计算器,准备用在项目里 http://www.cnblogs.com/blog5277/p/5707304.html 由于项目是用户计算跟钱有关的,所以这可不敢出BUG 于是就用了JU ...
- 《OOAD与UML那点儿事》目录索引
关键字:OOAD.UML.设计模式 各位园友,大家好,我是Bobby,在学习OOAD和开发的项目的过程中有一些感悟和想法,整理和编写了一些学习资料 [内容简介]掌握某种开发语言,让你实现了由零到一的脱 ...
- java 执行jar指定log4j.properties文件位置
默认情况下,log4j.properties会被加载,并且这个文件需要在classpath根目录,当打包jar时,会打包打jar内部,当需要修改日志级别时,会比较麻烦 可以使用:java -jar - ...
- [linux] scp无密码拷贝
源服务器为s,ip为111.111.111.112. 目标服务器为d, ip为111.111.111.111 1>在源服务器新建用户 test_s, useradd test_s -g user ...
- ximalaya