使用方法1

显示简单的文本

  1. 在layout文件中像加入普通控件一样在layout文件中引入ListView

    <ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </ListView>
  2. 数组中的数据要通过适配器来导入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

显示定制的界面

  1. 定义子项的构成

    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;
    }
    }
  2. 然后需要为子项写好布局
<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();
}
 

最新文章

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

热门文章

  1. Digital Roots
  2. Javascript学习笔记:3种定义函数的方式
  3. Bootstrap之Carousel不能自动播放的解决办法(转)
  4. bootstrap笔记
  5. MFC编程入门之二十七(常用控件:图片控件PictureControl)
  6. bootstrap 实战入门教程(一)
  7. Spring官网下载dist.zip的几种方法
  8. 分布式消息队列 Kafka
  9. WPF中RDLC报表的钻取实现
  10. secureCRT远程登录工具的颜色配置(转载)