赞
踩
微信公众号小卡片,ListView中嵌套CardView
- public class MainActivity extends AppCompatActivity {
-
- private ListView mLvMsgList;
- private List<Msg> mDatas = new ArrayList<>();//msg容器
- private MsgAdapter mAdapter;//适配器
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mLvMsgList = findViewById(R.id.id_lv_msgList);
- //把数据一一对应加进去
- mDatas.addAll(MsgLab.generateMockList());
- //实例化适配器对象
- mAdapter = new MsgAdapter(this, mDatas);
- //给ListView设置适配器
- mLvMsgList.setAdapter(mAdapter);
- }
- }
- /*
- 自定义适配器
- */
-
- 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 Msg getItem(int position) {
- return mDatas.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
-
- ViewHolder viewHolder;
-
- //获取ViewHolder的对应控件的id
- if (convertView == null) {
- convertView = mInflater.inflate(R.layout.item_msg, parent, false);
- viewHolder = new ViewHolder();
- viewHolder.mIvImg = convertView.findViewById(R.id.id_iv_img);
- viewHolder.mTvTitle = convertView.findViewById(R.id.id_tv_title);
- viewHolder.mTvContent = convertView.findViewById(R.id.id_tv_content);
- convertView.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) convertView.getTag();
- }
-
- //把传入的参数List<Msg>的数据一一拿出来给对应控件赋值
- Msg msg = mDatas.get(position);
- viewHolder.mIvImg.setImageResource(msg.getImgResId());
- viewHolder.mTvTitle.setText(msg.getTitle());
- viewHolder.mTvContent.setText(msg.getContent());
-
- return convertView;
- }
-
- //存储控件,便于重复取用,优化性能
- public static class ViewHolder {
- ImageView mIvImg;
- TextView mTvTitle;
- TextView mTvContent;
-
- }
- }
-
- /*
- Msg类
- */
-
- public class Msg {
-
- private int id;
- private int imgResId;
- private String title;
- private String content;
-
- public Msg() {
-
- }
-
- 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;
- }
- }
-
- /*
- 数据库类 自定义一个方法,返回值为Lis类型
- */
-
- public class MsgLab {
-
- //该方法把数据添加到List<Msg>
- public static List<Msg> generateMockList() {
- List<Msg> msgList = new ArrayList<>();
-
- //id,title,content三种信息
-
- 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.img03,
- "狗粮不是你想吃,就能吃的!",
- "你的朋友圈开始了吗?一半秀恩爱,一半扮感伤!不怕,还有慕课网陪你坚强地走下去!!");
- msgList.add(msg);
-
- msg = new Msg(4,
- R.drawable.img04,
- "前端跳槽面试那些事儿",
- "工作有几年了,项目偏简单有点拿不出手怎么办? 目前还没毕业,正在自学前端,请问可以找到一份前端工作吗,我该怎么办?");
- msgList.add(msg);
-
- msg = new Msg(5,
- R.drawable.img05,
- "图解程序员怎么过七夕?",
- "哈哈哈哈,活该单身25年!");
- msgList.add(msg);
-
- return msgList;
- }
-
- }
- <?xml version="1.0" encoding="utf-8"?>
- <ListView xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/id_lv_msgList"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffffff"
- android:divider="@null"
- android:paddingTop="8dp"
- tools:context="MainActivity">
-
- </ListView>
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout 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="wrap_content">
-
- <androidx.cardview.widget.CardView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="@dimen/margin_item_msg_l_r"
- android:layout_marginTop="@dimen/margin_item_msg_t_b"
- android:layout_marginRight="@dimen/margin_item_msg_l_r"
- android:layout_marginBottom="@dimen/margin_item_msg_t_b"
- android:foreground="?attr/selectableItemBackground"
- app:cardCornerRadius="8dp"
- app:cardElevation="4dp"
- app:cardPreventCornerOverlap="true"
- app:cardUseCompatPadding="false">
-
- <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="16sp"
- 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>
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。