当前位置:   article > 正文

CardView_scardview

scardview
微信公众号小卡片,ListView中嵌套CardView
  1. public class MainActivity extends AppCompatActivity {
  2. private ListView mLvMsgList;
  3. private List<Msg> mDatas = new ArrayList<>();//msg容器
  4. private MsgAdapter mAdapter;//适配器
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. mLvMsgList = findViewById(R.id.id_lv_msgList);
  10. //把数据一一对应加进去
  11. mDatas.addAll(MsgLab.generateMockList());
  12. //实例化适配器对象
  13. mAdapter = new MsgAdapter(this, mDatas);
  14. //给ListView设置适配器
  15. mLvMsgList.setAdapter(mAdapter);
  16. }
  17. }
  1. /*
  2. 自定义适配器
  3. */
  4. public class MsgAdapter extends BaseAdapter {
  5. private Context mContext;
  6. private LayoutInflater mInflater;
  7. private List<Msg> mDatas;
  8. //
  9. public MsgAdapter(Context context, List<Msg> datas) {
  10. mContext = context;
  11. mInflater = LayoutInflater.from(context);//加载布局资源
  12. mDatas = datas;
  13. }
  14. @Override
  15. public int getCount() {
  16. return mDatas.size();
  17. }
  18. @Override
  19. public Msg getItem(int position) {
  20. return mDatas.get(position);
  21. }
  22. @Override
  23. public long getItemId(int position) {
  24. return position;
  25. }
  26. @Override
  27. public View getView(int position, View convertView, ViewGroup parent) {
  28. ViewHolder viewHolder;
  29. //获取ViewHolder的对应控件的id
  30. if (convertView == null) {
  31. convertView = mInflater.inflate(R.layout.item_msg, parent, false);
  32. viewHolder = new ViewHolder();
  33. viewHolder.mIvImg = convertView.findViewById(R.id.id_iv_img);
  34. viewHolder.mTvTitle = convertView.findViewById(R.id.id_tv_title);
  35. viewHolder.mTvContent = convertView.findViewById(R.id.id_tv_content);
  36. convertView.setTag(viewHolder);
  37. } else {
  38. viewHolder = (ViewHolder) convertView.getTag();
  39. }
  40. //把传入的参数List<Msg>的数据一一拿出来给对应控件赋值
  41. Msg msg = mDatas.get(position);
  42. viewHolder.mIvImg.setImageResource(msg.getImgResId());
  43. viewHolder.mTvTitle.setText(msg.getTitle());
  44. viewHolder.mTvContent.setText(msg.getContent());
  45. return convertView;
  46. }
  47. //存储控件,便于重复取用,优化性能
  48. public static class ViewHolder {
  49. ImageView mIvImg;
  50. TextView mTvTitle;
  51. TextView mTvContent;
  52. }
  53. }
  1. /*
  2. Msg类
  3. */
  4. public class Msg {
  5. private int id;
  6. private int imgResId;
  7. private String title;
  8. private String content;
  9. public Msg() {
  10. }
  11. public Msg(int id, int imgResId, String title, String content) {
  12. this.id = id;
  13. this.imgResId = imgResId;
  14. this.title = title;
  15. this.content = content;
  16. }
  17. public int getId() {
  18. return id;
  19. }
  20. public void setId(int id) {
  21. this.id = id;
  22. }
  23. public int getImgResId() {
  24. return imgResId;
  25. }
  26. public void setImgResId(int imgResId) {
  27. this.imgResId = imgResId;
  28. }
  29. public String getTitle() {
  30. return title;
  31. }
  32. public void setTitle(String title) {
  33. this.title = title;
  34. }
  35. public String getContent() {
  36. return content;
  37. }
  38. public void setContent(String content) {
  39. this.content = content;
  40. }
  41. }
  1. /*
  2. 数据库类 自定义一个方法,返回值为Lis类型
  3. */
  4. public class MsgLab {
  5. //该方法把数据添加到List<Msg>
  6. public static List<Msg> generateMockList() {
  7. List<Msg> msgList = new ArrayList<>();
  8. //id,title,content三种信息
  9. Msg msg = new Msg(1,
  10. R.drawable.img01,
  11. "如何才能不错过人工智能的时代?",
  12. "下一个时代就是机器学习的时代,慕课网发大招,与你一起预见未来!");
  13. //
  14. msgList.add(msg);
  15. msg = new Msg(2,
  16. R.drawable.img02,
  17. "关于你的面试、实习心路历程",
  18. "奖品丰富,更设有参与奖,随机抽取5名幸运用户,获得慕课网付费面试课程中的任意一门!");
  19. msgList.add(msg);
  20. msg = new Msg(3,
  21. R.drawable.img03,
  22. "狗粮不是你想吃,就能吃的!",
  23. "你的朋友圈开始了吗?一半秀恩爱,一半扮感伤!不怕,还有慕课网陪你坚强地走下去!!");
  24. msgList.add(msg);
  25. msg = new Msg(4,
  26. R.drawable.img04,
  27. "前端跳槽面试那些事儿",
  28. "工作有几年了,项目偏简单有点拿不出手怎么办? 目前还没毕业,正在自学前端,请问可以找到一份前端工作吗,我该怎么办?");
  29. msgList.add(msg);
  30. msg = new Msg(5,
  31. R.drawable.img05,
  32. "图解程序员怎么过七夕?",
  33. "哈哈哈哈,活该单身25年!");
  34. msgList.add(msg);
  35. return msgList;
  36. }
  37. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ListView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/id_lv_msgList"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#ffffff"
  8. android:divider="@null"
  9. android:paddingTop="8dp"
  10. tools:context="MainActivity">
  11. </ListView>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout 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="wrap_content">
  7. <androidx.cardview.widget.CardView
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_marginLeft="@dimen/margin_item_msg_l_r"
  11. android:layout_marginTop="@dimen/margin_item_msg_t_b"
  12. android:layout_marginRight="@dimen/margin_item_msg_l_r"
  13. android:layout_marginBottom="@dimen/margin_item_msg_t_b"
  14. android:foreground="?attr/selectableItemBackground"
  15. app:cardCornerRadius="8dp"
  16. app:cardElevation="4dp"
  17. app:cardPreventCornerOverlap="true"
  18. app:cardUseCompatPadding="false">
  19. <LinearLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:orientation="vertical">
  23. <ImageView
  24. android:id="@+id/id_iv_img"
  25. android:layout_width="match_parent"
  26. android:layout_height="150dp"
  27. android:scaleType="centerCrop"
  28. tools:src="@drawable/img01" />
  29. <TextView
  30. android:id="@+id/id_tv_title"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_margin="8dp"
  34. android:textColor="#000000"
  35. android:textSize="16sp"
  36. android:textStyle="bold"
  37. tools:text="使用慕课网学习Android技术" />
  38. <TextView
  39. android:id="@+id/id_tv_content"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_marginLeft="8dp"
  43. android:layout_marginRight="8dp"
  44. android:layout_marginBottom="8dp"
  45. tools:text="使用慕课网学习Android技术使用慕课网学习Android技术使用慕课网学习Android技术使用慕课网学习Android技术" />
  46. </LinearLayout>
  47. </androidx.cardview.widget.CardView>
  48. </FrameLayout>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/910247
推荐阅读
相关标签
  

闽ICP备14008679号