ListView原理

image.png

ListView的常用属性

image.png

ListView的使用

image.png

image.png
image.png

实现ListView的一般步骤
◆在布局文件中编写代码(xxx.xml)
◆在xml文件中添加ListView标签
◆Activity中编写代码
1.获取ListView对象
2.准备数据源
3.配置适配器
4.将适配器关联到ListView中
image.png

适配器

image.png

image.png

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ListView
  6. android:id="@+id/lv1"
  7. android:divider="#8cc18c"
  8. android:dividerHeight="10dp"
  9. android:entries="@array/names"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. </ListView>
  13. </LinearLayout>

使用ArrayAdapter实现文字列表

image.png

使用SimpleAdapter实现图文混排

image.png

image.png

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_margin="6dp"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ImageView
  8. android:layout_weight="1"
  9. android:layout_margin="5dp"
  10. android:id="@+id/logo"
  11. android:src="@mipmap/ic_launcher"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content" />
  14. <LinearLayout
  15. android:layout_weight="1"
  16. android:layout_margin="5dp"
  17. android:orientation="vertical"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content">
  20. <TextView
  21. android:id="@+id/title"
  22. android:text="王者荣耀"
  23. android:textSize="18dp"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content" />
  26. <TextView
  27. android:id="@+id/version"
  28. android:text="版本:1.8.8"
  29. android:textSize="12dp"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content" />
  32. <TextView
  33. android:id="@+id/size"
  34. android:text="大小:1998M"
  35. android:textSize="12dp"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content" />
  38. </LinearLayout>
  39. <Button
  40. android:layout_weight="1"
  41. android:layout_margin="5dp"
  42. android:id="@+id/btn1"
  43. android:text="卸载应用"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content" />
  46. </LinearLayout>
  1. package com.bluelesson.phoneandmessage;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.telecom.Call;
  7. import android.telephony.SmsManager;
  8. import android.view.View;
  9. import android.widget.Adapter;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.EditText;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14. import android.widget.TextView;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. public class MainActivity extends AppCompatActivity {
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. initListView();
  25. initListView1();
  26. }
  27. private void initListView1() {
  28. //1.获取界面对象
  29. ListView listView= findViewById(R.id.lv1);
  30. //2.准备数据源
  31. List<Map<String,Object>> maplist=new ArrayList<>();
  32. Map<String,Object> map1 = new HashMap<>();
  33. map1.put("logo",R.mipmap.ic_launcher);
  34. map1.put("title","王者荣耀");
  35. map1.put("version","版本:1.8.8");
  36. map1.put("size","大小:1878M");
  37. Map<String,Object> map2 = new HashMap<>();
  38. map2.put("logo",R.mipmap.ic_launcher);
  39. map2.put("title","炉石传说");
  40. map2.put("version","版本:2.8.8");
  41. map2.put("size","大小:788M");
  42. Map<String,Object> map3 = new HashMap<>();
  43. map3.put("logo",R.mipmap.ic_launcher);
  44. map3.put("title","极品飞车");
  45. map3.put("version","版本:3.8.8");
  46. map3.put("size","大小:2188M");
  47. maplist.add(map1);
  48. maplist.add(map2);
  49. maplist.add(map3);
  50. //3.创建适配器
  51. SimpleAdapter adapter = new SimpleAdapter(this,
  52. maplist,
  53. R.layout.main2,
  54. new String[]{"logo","title","version","size"},
  55. new int[]{R.id.logo,R.id.title,R.id.version,R.id.size}
  56. );
  57. //4.将适配器与ListView关联
  58. listView.setAdapter(adapter);
  59. }
  60. private void initListView() {
  61. //1.获取界面对象
  62. ListView listView= findViewById(R.id.lv1);
  63. //2.准备数据源
  64. String strings[] = {"上海","北京","山东","山西","内蒙古","北京","山东","山西","内蒙"};
  65. //3.创建适配器
  66. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
  67. this,
  68. android.R.layout.simple_list_item_1, //列表项的布局文件
  69. android.R.id.text1, //TextView资渊文件
  70. strings //数据
  71. );
  72. //4.将适配器与ListView关联
  73. listView.setAdapter(arrayAdapter);
  74. }
  75. }

image.png

自定义Adapter

image.png

MyAdapter

  1. package com.bluelesson.phoneandmessage;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Map;
  13. public class MyAdapter extends BaseAdapter {
  14. public MyAdapter(List<Map<String, Object>> mList, Context mContext) {
  15. this.mList = mList;
  16. this.mContext = mContext;
  17. }
  18. private List<Map<String,Object>> mList = new ArrayList<>();
  19. private Context mContext;
  20. @Override
  21. public int getCount() {
  22. return mList.size();
  23. }
  24. @Override
  25. public Object getItem(int position) {
  26. return mList.get(position);
  27. }
  28. @Override
  29. public long getItemId(int position) {
  30. return position;
  31. }
  32. @Override
  33. public View getView(int position, View convertView, ViewGroup viewGroup) {
  34. //输出日志
  35. Log.d("15pb-log","位置="+position+" convertView="+convertView);
  36. //1.加我一个列表项布局文件,获取界面对象
  37. //1.1获取布局管理器
  38. //1.2加载布局
  39. LayoutInflater layoutInflater = LayoutInflater.from(mContext);
  40. View view = layoutInflater.inflate(R.layout.main2,null);
  41. //2.将数据添加到布局对象中
  42. //2.1从列表中获取数据
  43. Map<String, Object>map=mList.get(position);
  44. int resId = (int)map.get("logo");
  45. String version = (String)map.get("version");
  46. String title = (String)map.get("title");
  47. String size = (String)map.get("size");
  48. //2.2获取界面对象
  49. ImageView iv_logo = view.findViewById(R.id.logo);
  50. TextView tv_title = view.findViewById(R.id.title);
  51. TextView tv_version = view.findViewById(R.id.version);
  52. TextView tv_size = view.findViewById(R.id.size);
  53. //2.3将数据设置到界面上
  54. iv_logo.setImageResource(resId);
  55. tv_title.setText(title);
  56. tv_version.setText(version);
  57. tv_size.setText(size);
  58. //3.返回对象
  59. return view;
  60. }
  61. }

MainActivity

  1. package com.bluelesson.phoneandmessage;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.telecom.Call;
  7. import android.telephony.SmsManager;
  8. import android.view.View;
  9. import android.widget.Adapter;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.EditText;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14. import android.widget.TextView;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. public class MainActivity extends AppCompatActivity {
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. //initListView();
  25. //initListView1();
  26. initListView2();
  27. }
  28. private void initListView2() {
  29. //1.获取界面对象
  30. ListView listView= findViewById(R.id.lv1);
  31. //2.准备数据源
  32. List<Map<String,Object>> mapList=new ArrayList<>();
  33. Map<String,Object> map1 = new HashMap<>();
  34. map1.put("logo",R.mipmap.ic_launcher);
  35. map1.put("title","王者荣耀");
  36. map1.put("version","版本:1.8.8");
  37. map1.put("size","大小:1878M");
  38. Map<String,Object> map2 = new HashMap<>();
  39. map2.put("logo",R.mipmap.ic_launcher);
  40. map2.put("title","炉石传说");
  41. map2.put("version","版本:2.8.8");
  42. map2.put("size","大小:788M");
  43. Map<String,Object> map3 = new HashMap<>();
  44. map3.put("logo",R.mipmap.ic_launcher);
  45. map3.put("title","极品飞车");
  46. map3.put("version","版本:3.8.8");
  47. map3.put("size","大小:2188M");
  48. mapList.add(map1);
  49. mapList.add(map2);
  50. mapList.add(map3);
  51. mapList.add(map1);
  52. mapList.add(map2);
  53. mapList.add(map3);
  54. //3.创建适配器
  55. MyAdapter adapter = new MyAdapter(mapList,this);
  56. //4.将适配器与ListView关联
  57. listView.setAdapter(adapter);
  58. }
  59. private void initListView1() {
  60. //1.获取界面对象
  61. ListView listView= findViewById(R.id.lv1);
  62. //2.准备数据源
  63. List<Map<String,Object>> maplist=new ArrayList<>();
  64. Map<String,Object> map1 = new HashMap<>();
  65. map1.put("logo",R.mipmap.ic_launcher);
  66. map1.put("title","王者荣耀");
  67. map1.put("version","版本:1.8.8");
  68. map1.put("size","大小:1878M");
  69. Map<String,Object> map2 = new HashMap<>();
  70. map2.put("logo",R.mipmap.ic_launcher);
  71. map2.put("title","炉石传说");
  72. map2.put("version","版本:2.8.8");
  73. map2.put("size","大小:788M");
  74. Map<String,Object> map3 = new HashMap<>();
  75. map3.put("logo",R.mipmap.ic_launcher);
  76. map3.put("title","极品飞车");
  77. map3.put("version","版本:3.8.8");
  78. map3.put("size","大小:2188M");
  79. maplist.add(map1);
  80. maplist.add(map2);
  81. maplist.add(map3);
  82. //3.创建适配器
  83. SimpleAdapter adapter = new SimpleAdapter(this,
  84. maplist,
  85. R.layout.main2,
  86. new String[]{"logo","title","version","size"},
  87. new int[]{R.id.logo,R.id.title,R.id.version,R.id.size}
  88. );
  89. //4.将适配器与ListView关联
  90. listView.setAdapter(adapter);
  91. }
  92. private void initListView() {
  93. //1.获取界面对象
  94. ListView listView= findViewById(R.id.lv1);
  95. //2.准备数据源
  96. String strings[] = {"上海","北京","山东","山西","内蒙古","北京","山东","山西","内蒙"};
  97. //3.创建适配器
  98. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
  99. this,
  100. android.R.layout.simple_list_item_1, //列表项的布局文件
  101. android.R.id.text1, //TextView资渊文件
  102. strings //数据
  103. );
  104. //4.将适配器与ListView关联
  105. listView.setAdapter(arrayAdapter);
  106. }
  107. }

image.png

优化getView

  1. @Override
  2. public View getView(int position, View convertView, ViewGroup viewGroup) {
  3. //输出日志
  4. Log.d("15pb-log","位置="+position+" convertView="+convertView);
  5. View view = null;
  6. //优化getView
  7. if(convertView==null){
  8. //1.加我一个列表项布局文件,获取界面对象
  9. //1.1获取布局管理器
  10. LayoutInflater layoutInflater = LayoutInflater.from(mContext);
  11. //1.2加载布局
  12. view = layoutInflater.inflate(R.layout.main2,null);
  13. //2.将数据添加到布局对象中
  14. //2.1从列表中获取数据
  15. Map<String, Object>map=mList.get(position);
  16. int resId = (int)map.get("logo");
  17. String version = (String)map.get("version");
  18. String title = (String)map.get("title");
  19. String size = (String)map.get("size");
  20. //2.2获取界面对象
  21. ImageView iv_logo = view.findViewById(R.id.logo);
  22. TextView tv_title = view.findViewById(R.id.title);
  23. TextView tv_version = view.findViewById(R.id.version);
  24. TextView tv_size = view.findViewById(R.id.size);
  25. //2.3将数据设置到界面上
  26. iv_logo.setImageResource(resId);
  27. tv_title.setText(title);
  28. tv_version.setText(version);
  29. tv_size.setText(size);
  30. }else {
  31. view = convertView;
  32. }
  33. //3.返回对象
  34. return view;
  35. }

封装

  1. package com.bluelesson.phoneandmessage;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Map;
  13. public class MyAdapter extends BaseAdapter {
  14. public MyAdapter(List<Map<String, Object>> mList, Context mContext) {
  15. this.mList = mList;
  16. this.mContext = mContext;
  17. }
  18. private List<Map<String,Object>> mList = new ArrayList<>();
  19. private Context mContext;
  20. @Override
  21. public int getCount() {
  22. return mList.size();
  23. }
  24. @Override
  25. public Object getItem(int position) {
  26. return mList.get(position);
  27. }
  28. @Override
  29. public long getItemId(int position) {
  30. return position;
  31. }
  32. public class ViewHoler {
  33. ImageView iv_logo;
  34. TextView tv_title;
  35. TextView tv_version;
  36. TextView tv_size;
  37. public ViewHoler(View view) {
  38. this. iv_logo=view.findViewById(R.id.logo);
  39. this. tv_title=view.findViewById(R.id.title);
  40. this. tv_version=view.findViewById(R. id.version);
  41. this. tv_size=view.findViewById(R. id.size);
  42. }
  43. }
  44. @Override
  45. public View getView(int position, View convertView, ViewGroup viewGroup) {
  46. //输出日志
  47. Log.d("15pb-log","位置="+position+" convertView="+convertView);
  48. View view = null;
  49. //优化getView
  50. if(convertView==null){
  51. //1.加我一个列表项布局文件,获取界面对象
  52. //1.1获取布局管理器
  53. LayoutInflater layoutInflater = LayoutInflater.from(mContext);
  54. //1.2加载布局
  55. view = layoutInflater.inflate(R.layout.main2,null);
  56. //2.将数据添加到布局对象中
  57. //2.1从列表中获取数据
  58. Map<String, Object>map=mList.get(position);
  59. int resId = (int)map.get("logo");
  60. String version = (String)map.get("version");
  61. String title = (String)map.get("title");
  62. String size = (String)map.get("size");
  63. //2.2获取界面对象
  64. ViewHoler viewHolder = new ViewHoler(view);
  65. //2.3将数据设置到界面上
  66. viewHolder.iv_logo.setImageResource(resId);
  67. viewHolder.tv_title.setText(title);
  68. viewHolder.tv_version.setText(version);
  69. viewHolder.tv_size.setText(size);
  70. }else {
  71. view = convertView;
  72. }
  73. //3.返回对象
  74. return view;
  75. }
  76. }

listView的监听

例子1

image.png

例子2

image.png

  1. package com.bluelesson.phoneandmessage;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Map;
  14. public class MyAdapter extends BaseAdapter {
  15. public MyAdapter(List<Map<String, Object>> mList, Context mContext) {
  16. this.mList = mList;
  17. this.mContext = mContext;
  18. }
  19. private List<Map<String,Object>> mList = new ArrayList<>();
  20. private Context mContext;
  21. @Override
  22. public int getCount() {
  23. return mList.size();
  24. }
  25. @Override
  26. public Object getItem(int position) {
  27. return mList.get(position);
  28. }
  29. @Override
  30. public long getItemId(int position) {
  31. return position;
  32. }
  33. public class ViewHoler {
  34. ImageView iv_logo;
  35. TextView tv_title;
  36. TextView tv_version;
  37. TextView tv_size;
  38. public ViewHoler(View view) {
  39. this. iv_logo=view.findViewById(R.id.logo);
  40. this. tv_title=view.findViewById(R.id.title);
  41. this. tv_version=view.findViewById(R. id.version);
  42. this. tv_size=view.findViewById(R. id.size);
  43. }
  44. }
  45. @Override
  46. public View getView(int position, View convertView, ViewGroup viewGroup) {
  47. //输出日志
  48. Log.d("15pb-log","位置="+position+" convertView="+convertView);
  49. View view = null;
  50. //优化getView
  51. if(convertView==null){
  52. //1.加我一个列表项布局文件,获取界面对象
  53. //1.1获取布局管理器
  54. LayoutInflater layoutInflater = LayoutInflater.from(mContext);
  55. //1.2加载布局
  56. view = layoutInflater.inflate(R.layout.main2,null);
  57. //2.将数据添加到布局对象中
  58. //2.1从列表中获取数据
  59. Map<String, Object>map=mList.get(position);
  60. int resId = (int)map.get("logo");
  61. String version = (String)map.get("version");
  62. String title = (String)map.get("title");
  63. String size = (String)map.get("size");
  64. //2.2获取界面对象
  65. ViewHoler viewHolder = new ViewHoler(view);
  66. //2.3将数据设置到界面上
  67. viewHolder.iv_logo.setImageResource(resId);
  68. viewHolder.tv_title.setText(title);
  69. viewHolder.tv_version.setText(version);
  70. viewHolder.tv_size.setText(size);
  71. }else {
  72. view = convertView;
  73. }
  74. //3.设置监听器
  75. Button button=view.findViewById(R.id.uninstallbtn);
  76. button.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View view) {
  79. Log.d("15pb-log","日志"+view.getId());
  80. }
  81. });
  82. //3.返回对象
  83. return view;
  84. }
  85. }

image.png

常用控件

◆单选按钮

image.png

◆下拉列表

image.png
image.png

◆自动完成提示框

image.png

image.png