ListView的缓存机制
listView在使用过程中需要注意item view的显示与缓存机制,即屏幕内显示,滑出屏幕就被会收到缓存,避免每次调用getView时都是通过inflate创建一个新的View对象,同时避免又在此view中通过findViewById找到对应的控件,利用ListView的缓存机制实现view的复用。
将数据放入ListView中显示,需要建立数据源与ListView之间的适配关系,也就是需要自定义适配器。
数据源:学生(姓名,班级,年龄)
public class Student {
private String name;
private String clas;
private int age;
public Student(String name, String clas, int age) {
this.name = name;
this.clas = clas;
this.age = age;
}
public Student(){
}
public String getName() {
return name;
}
public String getClas() {
return clas;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setClas(String clas) {
this.clas = clas;
}
public void setAge(int age) {
this.age = age;
}
}
ListView:样式layout.item_student
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvClass"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
&