ListView基本用法

这篇文章是针对Android的ListView的学习笔记。

1 准备布局

一个list_item.xml布局,一个包含list_item.xml的list_view.xml。例:

girl_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/girl_image"
android:src="@drawable/icon1"
android:layout_width="72dp"
android:layout_height="72dp"/>

<TextView
android:id="@+id/girl_name"
android:text="chiaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"/>

</LinearLayout>
list_view.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.luowenqing.a3_6_recycleviewtest.MainActivity">

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

2 自定义适配器

确定数据源类型。例如少女类Girl:

Girl.java
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 class Girl {
private String name;
private int imageId;

public Girl(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;
}
}

使用系统提供适配器或自定义适配器类Adapter,类ViewHolder根据需要定义为内部类或外部类,ConvertView,ViewHolder缓存优化。例GirlAdapter:

GirlAdapter.java
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
public class GirlAdapter extends ArrayAdapter {

private int resourceId;

public GirlAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List objects) {
super(context, resource, objects);
resourceId = resource;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Girl girl = (Girl) getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

viewHolder = new ViewHolder();
viewHolder.girlImage = (ImageView) view.findViewById(R.id.girl_image);
viewHolder.girlName = (TextView) view.findViewById(R.id.girl_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.girlImage.setImageResource(girl.getImageId());
viewHolder.girlName.setText(girl.getName());
return view;
}

class ViewHolder {
ImageView girlImage;
TextView girlName;
}
}

3 加载布局

调用类,对应布局list_view.xml;获取数据源传入Adapter中,Adapter类对应list_item.xml。

MainActivity.java
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
40
41
42
43
44
public class MainActivity extends AppCompatActivity {
private List<Girl> girlsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);

initGirls();

GirlAdapter girlAdapter = new GirlAdapter(MainActivity.this, R.layout.girl_item, girlsList);

ListView listView = (ListView) findViewById(R.id.list_view);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "I love "+girlsList.get(position).getName(),
Toast.LENGTH_SHORT).show();
}
});

listView.setAdapter(girlAdapter);
}
// 准备数据源
public void initGirls() {
for (int i = 0 ; i < 3; i++) {
Girl gir11 = new Girl("chiaki", R.drawable.icon1);
girlsList.add(gir11);
Girl gir12 = new Girl("setsuna", R.drawable.icon2);
girlsList.add(gir12);
Girl gir13 = new Girl("touma", R.drawable.icon3);
girlsList.add(gir13);
Girl gir14 = new Girl("koharu", R.drawable.icon4);
girlsList.add(gir14);
Girl gir15 = new Girl("mari", R.drawable.icon5);
girlsList.add(gir15);
Girl gir16 = new Girl("nayuki", R.drawable.icon6);
girlsList.add(gir16);
Girl gir17 = new Girl("aiko", R.drawable.icon7);
girlsList.add(gir17);
Girl gir18 = new Girl("nagisa", R.drawable.icon8);
girlsList.add(gir18);
}
}
}

4 插入 & 删除 & 局部刷新Item

局部更新找到对应更新的item项,如果整个item的子控件都需要更新,调用getView;只需更新某个子控件,可通过findViewById()找出对应控件,进行更新。
先重写list_view.xml布局,重写MainActivity.java模仿数据更新。

list_view.xml
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
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.luowenqing.a3_6_recycleviewtest.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/edit_fun"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"/>

<Button
android:id="@+id/button_fun"
android:text="function"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class MainActivity extends AppCompatActivity {

private List<Girl> girlsList = new ArrayList<>();
private ListView listView;
private Button funButton;
private EditText funEditText;
private GirlAdapter girlAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
initGirls();
girlAdapter = new GirlAdapter(MainActivity.this, R.layout.girl_item, girlsList);
listView = (ListView) findViewById(R.id.list_view);

funButton = (Button) findViewById(R.id.button_fun);
funEditText = (EditText) findViewById(R.id.edit_fun);

funButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = funEditText.getText().toString();
String[] arr = str.split(" ");
int position = Integer.parseInt(arr[1]);
switch (arr[0]) {
case "delete":
girlsList.remove(position);
girlAdapter.notifyDataSetChanged();
break;
case "insert":
girlsList.add(position, new Girl("にゃんぱすー"+arr[1], R.drawable.avt));
girlAdapter.notifyDataSetChanged();
break;
case "update":
girlsList.set(position, new Girl("にゃんぱすー"+arr[1], R.drawable.avt));
updateItem(position);
break;
}
}
});

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "I love "+girlsList.get(position).getName(),
Toast.LENGTH_SHORT).show();
}
});

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

return false;
}
});
listView.setAdapter(girlAdapter);
}

public void initGirls() {
for (int i = 0 ; i < 3; i++) {
Girl gir11 = new Girl("chiaki", R.drawable.icon1);
girlsList.add(gir11);
Girl gir12 = new Girl("setsuna", R.drawable.icon2);
girlsList.add(gir12);
Girl gir13 = new Girl("touma", R.drawable.icon3);
girlsList.add(gir13);
Girl gir14 = new Girl("koharu", R.drawable.icon4);
girlsList.add(gir14);
Girl gir15 = new Girl("mari", R.drawable.icon5);
girlsList.add(gir15);
Girl gir16 = new Girl("nayuki", R.drawable.icon6);
girlsList.add(gir16);
Girl gir17 = new Girl("aiko", R.drawable.icon7);
girlsList.add(gir17);
Girl gir18 = new Girl("nagisa", R.drawable.icon8);
girlsList.add(gir18);
}
}

public void updateItem(int position) {
int firstVisiblePos = listView.getFirstVisiblePosition();
int lastVisiblePos = listView.getLastVisiblePosition();
if (position >= firstVisiblePos && position <= lastVisiblePos) {
View view = listView.getChildAt(position - firstVisiblePos);
girlAdapter.getView(position, view, listView);
}
}
}

布局的优化,将item的高度设为定值可以减少getView()调用次数。