效果图
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
RelativeLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_list_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="13dp"
android:layout_marginLeft="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_list_title"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginTop="25dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="123"
android:textColor="#000"/>
<TextView
android:id="@+id/tv_list_text"
android:layout_width="200dp"
android:layout_height="25dp"
android:text="456"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"/>
LinearLayout>
LinearLayout>
Messageitem.java
package com.example.myapplication;
public class Messageitem {
private int imageid;
private String title;
private String text;
public Messageitem(int imageid, String title, String text) {
this.imageid = imageid;
this.title = title;
this.text = text;
}
public Messageitem() {
}
public int getImageid() {
return imageid;
}
public void setImageid(int imageid) {
this.imageid = imageid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
MessageAdapter.java
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class MessageAdapter extends BaseAdapter {
private List<Messageitem> data;
private Context context;
public MessageAdapter(List<Messageitem> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Messageitem messageitem = data.get(position);
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);
ImageView image = convertView.findViewById(R.id.iv_list_image);
TextView title = convertView.findViewById(R.id.tv_list_title);
TextView text = convertView.findViewById(R.id.tv_list_text);
image.setImageResource(messageitem.getImageid());
title.setText(messageitem.getTitle());
text.setText(messageitem.getText());
}
return convertView;
}
}
MainActivity.java
package com.example.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class Message extends Application {
private int[] imageid = {R.drawable.file,R.drawable.yyb,R.drawable.school,R.drawable.file,R.drawable.yyb,R.drawable.school,R.drawable.file,R.drawable.yyb,R.drawable.school,R.drawable.file,R.drawable.yyb,R.drawable.school};
private String[] title = {"文件传输","应用宝","乐帮校园","文件传输","应用宝","乐帮校园","文件传输","应用宝","乐帮校园","文件传输","应用宝","乐帮校园"};
private String[] text = {".","..","...","....",".....","......",".","..","...","....",".....","......"};
private List<Messageitem> list = new ArrayList<>();
private ListView listView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
listview = findViewById(R.id.message_list);
listView.setAdapter(new MessageAdapter(list,getActivity()));
}
private void init(){
for (int i = 0;i<title.length;i++){
Messageitem messageitem = new Messageitem(imageid[i],title[i],text[i]);
list.add(messageitem);
}
}
}