记住需要的几个东西
一个实体类
一个子项的布局
一个自定义adapter类
简单listview
修改主布局,添加listview控件
1
2
3
4
5
6
7
8
9
10
11
12
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>修改主活动
1
2
3
4
5
6
7
8
9
10
11
12
13public class MainActivity extends AppCompatActivity {
private String[] Data = {"苹果", "香蕉", "橙子", "西瓜", "梨", "葡萄", "菠萝", "草莓", "樱桃", "芒果", "苹果", "香蕉", "橙子", "西瓜", "梨", "葡萄", "菠萝", "草莓", "樱桃", "芒果"};
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); //加载适配器
}
}ArrayAdapter用泛型指定适配的数据类型,然后在构造函数中把要适配的数据传入:有3个参数
参数1:当前上下文
参数2:子项布局的ID,android.R.layout.simple_list_item_1是Android内置布局,里面只有一段TextView,用于简单地显示一段文本
参数3:要适配的数据
自定义listView界面
定义一个Fruit类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public 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;
}
}定义一个子项布局 fruit_item布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fruite_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/fruite_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>自定义一个适配器FruitAdapter,继承自ArrayAdpter<Fruit>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = getItem(position); // 获取当前项的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
ImageView fruitImage = view.findViewById(R.id.fruite_image);
TextView fruitName = view.findViewById(R.id.fruite_name);
fruitImage.setImageResource(fruit.getImageId()); //加载图片
fruitName.setText(fruit.getName());//加载名字
return view;
}
}这里LayoutInflater的inflate方法接收三个参数,第三个参数指定False,表示只让我们在父布局中声明的layout属性生效。但不会为这个View添加父布局,因为一旦View有了父布局后,他就不能再添加到ListView中了
修改MainActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
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 = findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits(){
for (int i = 0; i < 2; i++){
Fruit apple = new Fruit("苹果", R.drawable.apple);
fruitList.add(apple);
Fruit banana = new Fruit("香蕉", R.drawable.banana);
fruitList.add(banana);
Fruit orange = new Fruit("橙子", R.drawable.orange);
fruitList.add(orange);
Fruit watermelon = new Fruit("西瓜", R.drawable.watermelon);
fruitList.add(watermelon);
Fruit pear = new Fruit("梨", R.drawable.pear);
fruitList.add(pear);
Fruit grape = new Fruit("葡萄", R.drawable.grape);
fruitList.add(grape);
Fruit pineapple = new Fruit("菠萝", R.drawable.pineapple);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("草莓", R.drawable.strawberry);
fruitList.add(strawberry);
Fruit cherry = new Fruit("樱桃", R.drawable.cherry);
fruitList.add(cherry);
Fruit mango = new Fruit("芒果", R.drawable.mango);
fruitList.add(mango);
}
}
}
提升ListView运行效率
convertView缓存
getView()方法有个convertView参数,用来将之前加载好的布局进行缓存,以便之后重用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = getItem(position); // 获取当前项的Fruit实例
View view;
if (convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
} else {
view = convertView;
}
ImageView fruitImage = view.findViewById(R.id.fruite_image);
TextView fruitName = view.findViewById(R.id.fruite_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
java
ViewHolder缓存
现在,虽然不会重复加载布局,但是每次还是会findViewById()来获取一次控件的实例,所以要借助ViewHolder来优化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = getItem(position); // 获取当前项的Fruit实例
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruite_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruite_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
//定义一个ViewHolder类
class ViewHolder {
ImageView fruitImage;
TextView fruitName;
}新增一个内部类ViewHolder,用于对实例进行缓存,将实例对象通过View.setTag()存在View中,这样就没必要每次都findViewById()了
listview点击事件
通过setOnItemClickListener()方法注册监听器:
1 |
|