Android Studio

Android—— ListView 的简单用法及定制ListV

2020-03-09  本文已影响0人  小羊学编程

一、ListView的简单用法

训练目标

  1. 掌握 ListView 控件的使用
  2. 掌握 Adapter 桥梁的作用

实现步骤:

1.首先新建一个项目, 并让ADT 自动帮我们创建好活动。然后修改activity_main.xml 中的代码,如下所示:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.edu.bu.a13lab07.MainActivity">
 
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view"
        >
    </ListView>
</LinearLayout>

在布局中加入ListView 控件,并为ListView 指定了一个id 设置成match_parent 占满整个空间。

2.在MainActivity 中:

public class MainActivity extends Activity {  
private String[] data = { "Apple", "Banana", "Orange", "Watermelon",  
                "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(  
                  MainActivity.this, android.R.layout.simple_list_item_1, data);  
     ListView listView = (ListView) findViewById(R.id.list_view);  
    listView.setAdapter(adapter);  
}  
}  
运行效果图: image.png

二、定制ListView界面

1.训练目标

  1. 主要参考步骤及代码

1、定义一个实体类Fruit

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、为 ListView 的子项指定一个我们自定义的布局 fruit_item.xml。

<?xml version="1.0" encoding="utf-8"?>
<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、创建一个自定义的适配器 FruitAdapter,这个适配器继承自 ArrayAdapter。重写构造方法和 getView 方法。


public class FruitAdapter extends ArrayAdapter{
    private final int resourceId;
 
    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = (Fruit) getItem(position); // 获取当前项的Fruit实例
        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);//获取该布局内的图片视图
        TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);//获取该布局内的文本视图
        fruitImage.setImageResource(fruit.getImageId());//为图片视图设置图片资源
        fruitName.setText(fruit.getName());//为文本视图设置文本内容
        return view;
    }
}     

View view = LayoutInflater.from(getContext()).inflate(resourceId, null);使用Inflater对象来将布局文件解析成一个View

4、在MainActivity中编写,初始化水果数据

public class MainActivity extends Activity {
    private List<Fruit> fruitList = new ArrayList<Fruit>();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits(); // 初始化水果数据
        FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }
 
    private void initFruits() {
        Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
        fruitList.add(apple);
        Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
        fruitList.add(banana);
        Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
        fruitList.add(orange);
        Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
        fruitList.add(watermelon);
        Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
        fruitList.add(pear);
        Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
        fruitList.add(grape);
        Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
        fruitList.add(pineapple);
        Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
        fruitList.add(strawberry);
        Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
        fruitList.add(cherry);
        Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
        fruitList.add(mango);
    }
}

5、运行效果图

image.png

这样一个简单的ListView界面就完成了。

虽然说效果是实现了,但是ListView还有许多可以优化的地方,其中运行效率就是很重要的一点。我们目前的运行效率是很低的,因为在FruitAdapter的getView方法中每次都把布局重新加载了一遍,当ListView快速滚动的时候,这就会成为性能的瓶颈。

我们注意到getView还有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,之后可以进行重用。现在,修改FruitAdapter的代码:

public class FruitAdapter extends ArrayAdapter{
    private final int resourceId;
 
    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = (Fruit) getItem(position); // 获取当前项的Fruit实例
        View view;
        if(convertView == null){
                view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
        }else{
                view = convertView;
        }
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);//获取该布局内的图片视图
        TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);//获取该布局内的文本视图
        fruitImage.setImageResource(fruit.getImageId());//为图片视图设置图片资源
        fruitName.setText(fruit.getName());//为文本视图设置文本内容
        return view;
    }
}     

我们在getView的方法中进行了判断,如果convertView为null,则使用LayoutInflater去加载布局,如果不为null,则直接对convertView进行重用。这样就大大提高了ListView的运行效率,在快速滚动的时候也可以表现更好的性能。

不过,我们目前的代码还可以继续优化,虽然现在不会重新去加载布局,但每次getView的时候还是会调用View的findViewById来获取控件的实例,我们可以借助ViewHolder来对这部分性能进行优化,修改FruitAdapter的代码如下:

public class FruitAdapter extends ArrayAdapter{
    private final int resourceId;
 
    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    ViewHolder  viewHolder;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = (Fruit) getItem(position); // 获取当前项的Fruit实例
        View view;
        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);//将ViewHolder存储在View中
        }else{
                view = convertView;
        }
        fruitImage.setImageResource(fruit.getImageId());//为图片视图设置图片资源
        fruitName.setText(fruit.getName());//为文本视图设置文本内容
        return view;
    }
        class ViewHolder{
             ImageView fruitImage;
            TextView fruitName; 
        }
}     

上一篇 下一篇

猜你喜欢

热点阅读