效果图
1.activity_main.xml页面
1 <ListView
2 android:layout_width="match_parent"
3 android:layout_height="460dp"
4 android:id="@+id/list_item">
5
6 </ListView>
2.同级layout目录下创建一个item_list.xml页面,编辑布局
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:background="#fff"
8 android:orientation="vertical"
9 android:padding="10dp"
10 >
11
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:gravity="center_vertical"
16 >
17
18
19 <LinearLayout
20 android:layout_width="0dp"
21 android:layout_height="wrap_content"
22 android:layout_weight="6"
23 android:orientation="vertical"
24 >
25
26 <TextView
27 android:id="@+id/title"
28 android:layout_width="wrap_content"
29 android:layout_height="wrap_content"
30 android:textColor="#000"
31 android:textSize="18sp" />
32
33 <LinearLayout
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:layout_marginTop="10dp"
37 android:layout_marginBottom="10dp">
38
39 <ImageView
40 android:id="@+id/icon"
41 android:layout_width="20dp"
42 android:layout_height="20dp"/>
43
44 <TextView
45 android:layout_width="wrap_content"
46 android:layout_height="wrap_content"
47 android:id="@+id/warning"
48 android:textColor="#cccc33"
49 android:textSize="16sp" />
50 </LinearLayout>
51
52
53 <TextView
54 android:id="@+id/time"
55 android:layout_width="wrap_content"
56 android:layout_height="wrap_content"
57 android:textColor="#666"
58 android:textSize="14sp" />
59
60 </LinearLayout>
61
62 <Button
63 android:layout_width="0dp"
64 android:layout_height="48dp"
65 android:layout_weight="1"
66 android:background="@drawable/radius"
67 android:text="确认" />
68
69 </LinearLayout>
70
71
72 </RelativeLayout>
3.MainActivity.java
1 package com.rxy.machine.activity;
2
3 import androidx.appcompat.app.AppCompatActivity;
4
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.widget.BaseAdapter;
9 import android.widget.ImageView;
10 import android.widget.ListView;
11 import android.widget.TextView;
12
13 import com.rxy.machine.R;
14
15 public class MovingActivity extends AppCompatActivity {
16 private ListView listItem;
17 private ImageView icon ;
18 private TextView title,warning,time;
19 // 1.定义适配器中的数组内容
20 private int[] icons = {R.drawable.jinggao1,R.drawable.jinggao2,R.drawable.jinggao3};
21 private String[] titles ={"除湿机通讯失败","温湿度温度高报警","门开"};
22 private String[] warnings ={"一般报警","次要报警","主要报警"};
23 private String[] times ={"2021-08-05 09:58:21","2021-08-05 09:58:21","2021-08-05 09:58:21"};
24
25
26 @Override
27 protected void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.activity_moving);
30 listItem = findViewById(R.id.list_item);
31 MyAdapter md = new MyAdapter();
32 listItem.setAdapter(md);
33 }
34 private class MyAdapter extends BaseAdapter {
35
36 @Override
37 public int getCount() { // 获取item的数量
38 return titles.length;
39 }
40
41 @Override
42 public Object getItem(int i) { //记录当前item的序号,从0开始,getItem获取某个item对象
43 return titles[i];
44 }
45
46 @Override
47 public long getItemId(int i) { //获取到选中的item项的编号
48 return i;
49 }
50
51 @Override //用于适配listview要加载的内容,包括视图和数据
52 public View getView(int i, View convertView, ViewGroup viewGroup) {
53 // 将item布局文件解析成一个view对象
54 // View view =View.inflate(MovingActivity.this,R.layout.item_list,null);
55 // title= view.findViewById(R.id.title);
56 // warning= view.findViewById(R.id.warning);
57 // time= view.findViewById(R.id.time);
58 // icon= view.findViewById(R.id.icon);
59 //
60 // title.setText(titles[i]);
61 // warning.setText(warnings[i]);
62 // time.setText(times[i]);
63 // icon.setBackgroundResource(icons[i]);
64
65 // return view;
66
67
68
69 //优化
70 Object holder =null;
71 // convertView 用于缓存item的对象
72 if(convertView==null){
73 convertView=View.inflate(MovingActivity.this,R.layout.item_list,null);
74 holder = new ViewHolder();
75 ((ViewHolder) holder).title = convertView.findViewById(R.id.title);
76 ((ViewHolder) holder).warning = convertView.findViewById(R.id.warning);
77 ((ViewHolder) holder).time = convertView.findViewById(R.id.time);
78 ((ViewHolder) holder).icon = convertView.findViewById(R.id.icon);
79 convertView.setTag(holder);
80
81 }else {
82 holder = convertView.getTag();
83 }
84 ((ViewHolder) holder).icon.setBackgroundResource(icons[i]);
85 ((ViewHolder) holder).title.setText(titles[i]);
86 ((ViewHolder) holder).warning.setText(warnings[i]);
87 ((ViewHolder) holder).time.setText(times[i]);
88 return convertView;
89 }
90
91
92 }
93 static class ViewHolder{
94 TextView title;
95 TextView time;
96 TextView warning;
97 ImageView icon;
98 }
99 }