Android中ListView的用法

使用方法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();
        }
 
原文地址:https://www.cnblogs.com/dracohan/p/5976968.html