<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_lv_msgList"
android:divider="@null"
android:background="#ffffff"
android:paddingTop="8dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
</ListView>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
app:cardUseCompatPadding="false"
android:foreground="?attr/selectableItemBackground"
app:cardPreventCornerOverlap="true"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/id_iv_img"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
tools:src="@drawable/img01"/>
<TextView
android:id="@+id/id_tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textColor="#000000"
android:textSize="16dp"
android:textStyle="bold"
tools:text="使用慕课网学习Android技术"
/>
<TextView
android:id="@+id/id_tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
tools:text="使用慕课网学习Android技术使用慕课网学习Android技术使用慕课网学习Android技术使用慕课网学习Android技术"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
实体类
package com.example.testapplication.bean;
/**
* 实体类
* */
public class Msg {
private int id;
private int imgResId;
private String title;
private String content;
public Msg(int id, int imgResId, String title, String content) {
this.id = id;
this.imgResId = imgResId;
this.title = title;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getImgResId() {
return imgResId;
}
public void setImgResId(int imgResId) {
this.imgResId = imgResId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
方法
package com.example.testapplication.bean;
import com.example.testapplication.R;
import java.util.ArrayList;
import java.util.List;
public class MsgLab {
public static List<Msg> generateMockList() {
List<Msg> msgList = new ArrayList<>();
Msg msg = new Msg(1, R.drawable.img01,
"如何才能不错过人工智能的时代?",
"下一个时代就是机器学习的时代,慕课网发大招,与你一起预见未来!");
msgList.add(msg);
msg = new Msg(2,
R.drawable.img02,
"关于你的面试、实习心路历程",
"奖品丰富,更设有参与奖,随机抽取5名幸运用户,获得慕课网付费面试课程中的任意一门!");
msgList.add(msg);
msg = new Msg(3,
R.drawable.img04,
"狗粮不是你想吃,就能吃的!",
"你的朋友圈开始了吗?一半秀恩爱,一半扮感伤!不怕,还有慕课网陪你坚强地走下去!!");
msgList.add(msg);
msg = new Msg(4,
R.drawable.img04,
"前端跳槽面试那些事儿",
"工作有几年了,项目偏简单有点拿不出手怎么办? 目前还没毕业,正在自学前端,请问可以找到一份前端工作吗,我该怎么办?");
msgList.add(msg);
msg = new Msg(5,
R.drawable.img01,
"图解程序员怎么过七夕?",
"哈哈哈哈,活该单身25年!");
msgList.add(msg);
return msgList;
}
}
适配器
package com.example.testapplication.adapter;
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 com.example.testapplication.R;
import com.example.testapplication.bean.Msg;
import java.util.List;
public class MsgAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private List<Msg> mDatas;
public MsgAdapter(Context context, List<Msg> datas) {
mContext = context;
mInflater = LayoutInflater.from(context);
mDatas = datas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int i) {
return mDatas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
view = mInflater.inflate(R.layout.activity_carditem,viewGroup,false);
viewHolder = new ViewHolder();
viewHolder.mIvImg = view.findViewById(R.id.id_iv_img);
viewHolder.mTvTitle = view.findViewById(R.id.id_tv_title);
viewHolder.mTvContent = view.findViewById(R.id.id_tv_content);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
Msg msg = mDatas.get(i);
viewHolder.mIvImg.setImageResource(msg.getImgResId());
viewHolder.mTvTitle.setText(msg.getTitle());
viewHolder.mTvContent.setText(msg.getContent());
return view;
}
public static class ViewHolder {
ImageView mIvImg;
TextView mTvTitle;
TextView mTvContent;
}
}
package com.example.testapplication;
import android.os.Bundle;
import android.widget.ListView;
import androidx.annotation.Nullable;
import com.example.testapplication.adapter.MsgAdapter;
import com.example.testapplication.bean.Msg;
import com.example.testapplication.bean.MsgLab;
import java.util.ArrayList;
import java.util.List;
public class CardViewActivity extends ApplicationActivity{
private ListView mLvMsgList;
private List<Msg> mDatas = new ArrayList<>();
private MsgAdapter mAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card);
mLvMsgList = findViewById(R.id.id_lv_msgList);
mDatas.addAll(MsgLab.generateMockList());
mDatas.addAll(MsgLab.generateMockList());
mAdapter = new MsgAdapter(this, mDatas);
mLvMsgList.setAdapter(mAdapter);
}
}