ListView 复杂列表 聊天界面 消息类型

时间:2021-09-30 19:36:25
Demo:https://github.com/baiqiantao/MultiTypeTest.git

addHeaderView方式

Activity 

   
   
  1. public class Activity1 extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. ListView listView = new ListView(this);
  6. List<Model1> mList = new ArrayList<>();
  7. for (int i = 0; i < 100; i++) {
  8. mList.add(new Model1(("包青天" + i), R.drawable.icon));
  9. }
  10. //给ListView添加头尾
  11. TextView mTextView = new TextView(this);
  12. mTextView.setText("我是头部\n必须在listview.setAdapter前添加");
  13. mTextView.setBackgroundColor(Color.YELLOW);
  14. listView.addHeaderView(mTextView);//必须在listview.setAdapter前添加。添加以后,listView的position=0的View是此View
  15. ImageView mImageView = new ImageView(this);
  16. mImageView.setImageResource(R.drawable.icon);
  17. mImageView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 300));
  18. listView.addHeaderView(mImageView);
  19. listView.setHeaderDividersEnabled(new Random().nextBoolean());//控制头部是否显示分割线。默认为true
  20. View footerView = new View(this);
  21. footerView.setBackgroundColor(Color.GREEN);
  22. footerView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 50));
  23. listView.addFooterView(footerView);
  24. listView.setFooterDividersEnabled(new Random().nextBoolean());
  25. listView.setAdapter(new MyAdapter1(this, mList));//addHeaderView要放在setAdapter之前,而addFooterView放在前后都可以
  26. listView.setDivider(new ColorDrawable(Color.RED));
  27. listView.setDividerHeight(2);//如果调用了setDivider,也需调用setDividerHeight才行
  28. listView.setOnItemClickListener((parent, view, p, id) -> Toast.makeText(this, "p=" + p, Toast.LENGTH_SHORT).show());
  29. setContentView(listView);
  30. }
  31. }

Adapter

   
   
  1. public class MyAdapter1 extends BaseAdapter {
  2. private Context mContext;
  3. private List<Model1> mList;
  4. public MyAdapter1(Context context, List<Model1> list) {
  5. this.mContext = context;
  6. this.mList = list;
  7. }
  8. @Override
  9. public int getCount() {
  10. return mList.size();
  11. }
  12. @Override
  13. public Object getItem(int position) {
  14. return mList.get(position);
  15. }
  16. @Override
  17. public long getItemId(int position) {
  18. return position;
  19. }
  20. @Override
  21. public View getView(int position, View convertView, ViewGroup parent) {
  22. ViewHolder mViewHolder;
  23. if (convertView != null) {
  24. mViewHolder = (ViewHolder) convertView.getTag();
  25. } else {
  26. convertView = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
  27. mViewHolder = new ViewHolder();
  28. mViewHolder.iv_head = (ImageView) convertView.findViewById(R.id.holder2_iv);
  29. mViewHolder.tv_name = (TextView) convertView.findViewById(R.id.holder2_title);
  30. convertView.setTag(mViewHolder);
  31. }
  32. Model1 mBean = mList.get(position);
  33. mViewHolder.iv_head.setImageResource(mBean.resId);
  34. mViewHolder.tv_name.setText(mBean.name + " position=" + position);
  35. return convertView;
  36. }
  37. public static class ViewHolder {
  38. public ImageView iv_head;// 头像
  39. public TextView tv_name;// 名字
  40. }
  41. }

Model

   
   
  1. public class Model1 {
  2. public String name;
  3. public int resId;
  4. public Model1(String name, int resId) {
  5. this.name = name;
  6. this.resId = resId;
  7. }
  8. }

getItemViewType方式

Activity 

   
   
  1. public class Activity2 extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. ListView listView = new ListView(this);
  6. List<Model2> mList = new ArrayList<>();
  7. for (int i = 0; i < 100; i++) {
  8. if (new Random().nextBoolean()) mList.add(new Model2(Model2.ITEM_FIRST, "第一种样式 " + i));
  9. else mList.add(new Model2(Model2.ITEM_SECOND, "第二种样式 " + i, R.drawable.icon));
  10. }
  11. listView.setAdapter(new MyAdapter2(this, mList));
  12. listView.setOnItemClickListener((parent, view, p, id) -> Toast.makeText(this, "p=" + p, Toast.LENGTH_SHORT).show());
  13. setContentView(listView);
  14. }
  15. }

Adapter【关键】

   
   
  1. public class MyAdapter2 extends BaseAdapter {
  2. private Context context;
  3. private List<Model2> mList;
  4. public MyAdapter2(Context context, List<Model2> list) {
  5. this.context = context;
  6. mList = list;
  7. }
  8. //**************************************************************************************************************************
  9. @Override
  10. public int getCount() {
  11. return mList.size();
  12. }
  13. @Override
  14. public Object getItem(int position) {
  15. return mList.get(position);
  16. }
  17. //重写方法一:返回值代表的是某一个样式的 Type(是一个需要我们自己定义的,用于区分不同样式的int类型的值)
  18. @Override
  19. public int getItemViewType(int position) {
  20. return mList.get(position).type;
  21. }
  22. //重写方法一:返回的是你有几种类型的样式
  23. @Override
  24. public int getViewTypeCount() {
  25. return 2;
  26. }
  27. @Override
  28. public long getItemId(int paramInt) {
  29. return paramInt;
  30. }
  31. @Override
  32. public View getView(int position, View convertView, ViewGroup parent) {
  33. int type = getItemViewType(position);
  34. //要使用不同类型的ViewHolder
  35. Holder1 holder1 = null;
  36. Holder2 holder2 = null;
  37. //************************************************初始化和复用******************************************
  38. if (convertView != null) {
  39. switch (type) {
  40. case Model2.ITEM_FIRST:
  41. holder1 = (Holder1) convertView.getTag();
  42. Log.i("bqt", position + " 复用 " + type);
  43. break;
  44. case Model2.ITEM_SECOND:
  45. holder2 = (Holder2) convertView.getTag();
  46. Log.i("bqt", position + " 复用 " + type);
  47. break;
  48. }
  49. } else {
  50. switch (type) {
  51. case Model2.ITEM_FIRST:
  52. convertView = View.inflate(context, R.layout.head, null);
  53. holder1 = new Holder1();
  54. holder1.holder1_title = (TextView) convertView.findViewById(R.id.holder1_title);
  55. holder1.holder1_time = (TextView) convertView.findViewById(R.id.holder1_time);
  56. convertView.setTag(holder1);
  57. Log.i("bqt", position + " 初始化 " + type);
  58. break;
  59. case Model2.ITEM_SECOND:
  60. convertView = View.inflate(context, R.layout.item, null);
  61. holder2 = new Holder2();
  62. holder2.holder2_title = (TextView) convertView.findViewById(R.id.holder2_title);
  63. holder2.holder2_iv = (ImageView) convertView.findViewById(R.id.holder2_iv);
  64. convertView.setTag(holder2);
  65. Log.i("bqt", position + " 初始化 " + type);
  66. break;
  67. }
  68. }
  69. //*************************************************填充数据*****************************************
  70. switch (type) {
  71. case Model2.ITEM_FIRST:
  72. if (holder1 != null) {
  73. holder1.holder1_title.setText(mList.get(position).title);
  74. holder1.holder1_time.setText(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()).format(new Date()));
  75. }
  76. break;
  77. case Model2.ITEM_SECOND:
  78. if (holder2 != null) {
  79. holder2.holder2_title.setText(mList.get(position).title);
  80. holder2.holder2_iv.setImageResource(mList.get(position).resId);
  81. }
  82. break;
  83. }
  84. return convertView;
  85. }
  86. //**************************************************************************************************************************
  87. private class Holder1 {
  88. TextView holder1_title;
  89. TextView holder1_time;
  90. }
  91. private class Holder2 {
  92. TextView holder2_title;
  93. ImageView holder2_iv;
  94. }
  95. }

Model

   
   
  1. public class Model2 {
  2. public static final int ITEM_FIRST = 0;//第一个样式
  3. public static final int ITEM_SECOND = 1;//第二个样式
  4. public int type;//记录是哪种样式
  5. public String title;//标题
  6. public int resId;//图片,仅第二个样式可以获取图片
  7. public Model2(int type, String str) {
  8. this.type = type;
  9. title = str;
  10. }
  11. public Model2(int type, String str, int resId) {
  12. this.type = type;
  13. title = str;
  14. this.resId = resId;
  15. }
  16. }

使用第三方框架 MultiType【推荐】

Demo:https://github.com/baiqiantao/MultiTypeTest.gitGitHub:https://github.com/drakeet/MultiType
     
     
  1. compile 'me.drakeet.multitype:multitype:3.1.0'
2017-7-18


null