最常用和最难用的控件 - ListView
ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据则会滚动出屏幕。
目录
一、定制ListView的界面
二、提升ListView的运行效率
1、定义一个实体类作为ListView适配器的适配类型
定义Fruit类
/*
* 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 void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
2、为ListView的子项指定一个自定义的布局
3、创建一个自定义的适配器
这个适配器继承自ArrayAdapter,并将泛型指定为Fruit类
tips: ArrayAdapter最大的优点是可以支持泛型构造,关于ArrayAdapter的介绍可以参考这篇文章:http://www.cnblogs.com/wenjiang/p/3196205.html
/*
* Fruit适配器类
* */
public class FruitAdapter extends ArrayAdapter {
private int resourceId;
public FruitAdapter(@NonNull Context context, int resource, @NonNull List objects) {
super(context, resource, objects);
resourceId = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = (Fruit) getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
ImageView fruitImage = view.findViewById(R.id.fruitImage);
TextView fruitName = view.findViewById(R.id.fruitName);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
4、在Activity中创建自定义的ListView界面并设置listview的点击事件
public class FruitActivity extends AppCompatActivity {
private List fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit);
ActionBar actionBar = getSupportActionBar();
if (actionBar!=null){
actionBar.hide();
}
TextView title = findViewById(R.id.title_text);
title.setText("ListViewDemo");
initFruit();
FruitAdapter fruitAdapter = new FruitAdapter(this, R.layout.fruit_layout,fruitList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(fruitAdapter);
//设置listview的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
Fruit fruit = fruitList.get(i);
//匿名内部类中,context不能用this
Toast.makeText(FruitActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
}
void initFruit(){
for (int i = 0; i < 100; i++){
Fruit apple = new Fruit("apple", R.drawable.apple_pic);
Fruit banana = new Fruit("banana", R.drawable.banana_pic);
fruitList.add(apple);
fruitList.add(banana);
}
}
}
布局文件:
性能优化点:
1、因为在FruitAdapter的getView()方法中,每次都会使用LayoutInflater动态的将布局重新加载一遍,而getView()方法在每个子项被滚动到屏幕内的时候都会被调用,当listview快速滚动的时候,这就会成为性能瓶颈;
2、每次在getView()方法中还会调用View的findViewById方法来获取一次控件的实例。
性能优化方法:
1、getView()方法中有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,以便之后可以进行重用,可以在动态加载布局之前判断一下converView是否已缓存,有缓存则直接使用缓存的数据,而不需要再次加载了;
2、可以借助一个自定义的ViewHolder类和View的setTag()、getTag() 方法,在view第一次加载的时候,将控件的实例缓存下来,后续再加载的时候判断一下是否有缓存,有缓存则直接使用缓存的数据。
代码实现如下:
/*
* Fruit适配器类
* */
public class FruitAdapter extends ArrayAdapter {
private int resourceId;
public FruitAdapter(@NonNull Context context, int resource, @NonNull List objects) {
super(context, resource, objects);
resourceId = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = (Fruit) getItem(position);
View view;
ViewHolder viewHolder;
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitName = view.findViewById(R.id.fruitName);
viewHolder.fruitImage = view.findViewById(R.id.fruitImage);
view.setTag(viewHolder);
}else{
view = convertView;
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
class ViewHolder{
TextView fruitName;
ImageView fruitImage;
}
}
tips:
1、静态成员变量
2、匿名内部类中用到的成员变量必须声明为final