Android中ListView的用法
2024-10-12 19:45:42
使用方法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();
}
最新文章
- 关于SMBIOS
- 有关vue的总结
- Android—SQLITE数据库的设计和升降级
- 如何解决inline-block元素的空白间距
- poj 2240 Arbitrage (最短路 bellman_ford)
- 解决tomcat占用8080端口问题
- Turtle库
- iOS之本地推送(前台模式与后台模式)
- Typora - Markdown 语法说明
- 简单的ALV示例
- [UE4]虚幻4链接独立服务器
- 对象 Object
- 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用(转载)
- Java怎么转义&;#1234;这种字符
- STORJ 有实际应用
- web worker的用法及应用场景(转)
- H5禁止手机自带键盘弹出
- Android ProgressBar 进度条荧光效果
- selenium + chrome 被检测,反反爬小记
- java的arrayCopy用法