ListView使用(三)BaseAdapter

自定义Adapter可以满足开发中的各种需求,所以,这种用法也是比较常见。

布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
</android.support.constraint.ConstraintLayout>

item文件

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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="900dp"
android:layout_height="140dp"
android:background="#FFF">
<TextView
android:id="@+id/tv_notice1_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="周五国内油价将迎来“两连涨”"
android:textColor="#690052"
android:textSize="35sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_notice1_content"
android:layout_width="match_parent"
android:layout_height="31dp"
android:layout_marginBottom="29dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="#f0dce3"
android:text="在国际原油六连涨"
android:textColor="#690052"
android:textSize="21sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_notice1_title" />
</android.support.constraint.ConstraintLayout>

创建数据

1
2
3
4
5
6
7
8
public class Notice {
public String notice_title;
public String notice_content;
public Notice(String notice_title, String notice_content){
this.notice_content = notice_content;
this.notice_title = notice_title;
}
}

创建适配器

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
public class Adapter_notice extends BaseAdapter {
private List<Notice> noticeList;
private Context mcontext;
private LayoutInflater inflater;//布局装载器对象
private int selectItem = -1;
private int currentFlag = 0;
public Adapter_notice(Context context,List<Notice> list){
super();
this.noticeList = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return noticeList.size();
}
@Override
public Object getItem(int position) {
return noticeList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.adapter_notice,null);
TextView title = view.findViewById(R.id.tv_notice1_title);
TextView content= view.findViewById(R.id.tv_notice1_content);
Notice notice = noticeList.get(position);
title.setText(notice.notice_title);
content.setText(notice.notice_content);

return view;
}
}

使用

1
2
3
4
5
6
7
8
9
//组装数据
List<Notice> itemBeanList = new ArrayList<>();
for (int i = 0;i < 4; i ++){
itemBeanList.add(new Notice("标题" + i, "内容" + i));
}
//实例化适配器(getactivity()是因为我在Fragment里使用,所以用这个)
Adapter_notice adapter_notice = new Adapter_notice(getActivity(),itemBeanList);
//使用
gridView.setAdapter(adapter_notice);

实现