经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android ListView使用方法以及注意事项
来源:cnblogs  作者:Ietree  时间:2020/12/14 17:02:46  对本文有异议

一、直接在布局文件中使用ListView

1、布局文件

  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. 3 xmlns:tools="http://schemas.android.com/tools"
  4. 4 android:layout_width="match_parent"
  5. 5 android:layout_height="match_parent"
  6. 6 tools:context=".MainActivity">
  7. 7
  8. 8 <ListView
  9. 9 android:id="@+id/lv_list"
  10. 10 android:layout_width="match_parent"
  11. 11 android:layout_height="match_parent" />
  12. 12
  13. 13 </RelativeLayout>

2、填充ListView代码

  1. 1 package com.ietree.listviewdemo;
  2. 2
  3. 3 import androidx.appcompat.app.AppCompatActivity;
  4. 4
  5. 5 import android.os.Bundle;
  6. 6 import android.view.View;
  7. 7 import android.view.ViewGroup;
  8. 8 import android.widget.BaseAdapter;
  9. 9 import android.widget.ListAdapter;
  10. 10 import android.widget.ListView;
  11. 11 import android.widget.TextView;
  12. 12
  13. 13 public class MainActivity extends AppCompatActivity {
  14. 14 private ListView lv_list;
  15. 15
  16. 16 @Override
  17. 17 protected void onCreate(Bundle savedInstanceState) {
  18. 18 super.onCreate(savedInstanceState);
  19. 19 setContentView(R.layout.activity_main);
  20. 20
  21. 21 // 获取ListView控件
  22. 22 lv_list = findViewById(R.id.lv_list);
  23. 23 // 给ListView控件设置自定义适配器
  24. 24 lv_list.setAdapter(new MyAdapter());
  25. 25 }
  26. 26
  27. 27 /**
  28. 28 * 定义一个适配器
  29. 29 */
  30. 30 private class MyAdapter extends BaseAdapter {
  31. 31 // 返回需要显示的条目数
  32. 32 @Override
  33. 33 public int getCount() {
  34. 34 return 100;
  35. 35 }
  36. 36
  37. 37 @Override
  38. 38 public Object getItem(int position) {
  39. 39 return null;
  40. 40 }
  41. 41
  42. 42 @Override
  43. 43 public long getItemId(int position) {
  44. 44 return 0;
  45. 45 }
  46. 46
  47. 47 /**
  48. 48 * 获取一个View用来显示ListView的数据,会作为ListView的一个条目显示
  49. 49 * @param position 当前需要显示的View的索引
  50. 50 * @param convertView 或存数据的对象
  51. 51 * @param parent
  52. 52 * @return 返回需要显示的View
  53. 53 */
  54. 54 @Override
  55. 55 public View getView(int position, View convertView, ViewGroup parent) {
  56. 56 TextView textView = new TextView(MainActivity.this);
  57. 57 textView.setText("这是第" + position + "个TextView");
  58. 58 return textView;
  59. 59 }
  60. 60 }
  61. 61 }

3、结果显示

 

4、几个需要注意的问题

1、ListView的布局不建议使用 

 

原因:

假如我们需要显示5个item,在使用 android:layout_height="wrap_content"的布局情况下,getView()方法会被调用9次,原因是他需要计算需要多少个item把当前界面填满,所以会循环调用多次。

但是如果使用 android:layout_height="match_parent" ,getView()方法就最多被调用5次。所以,一般推荐item在布局时使用android:layout_height="match_parent" 减少重复调用次数。

 2、假如需要显示的item有10000个,使用上面的每加载一个item就要新生成一个TextView对象的方法,会导致在滑动过程中加载内容过多导致卡顿或者内存溢出的情况,所以需要复用TextView对象。改造过后如下:

  1. /**
  2. * 获取一个View用来显示ListView的数据,会作为ListView的一个条目显示
  3. *
  4. * @param position 当前需要显示的View的索引
  5. * @param convertView 或存数据的对象
  6. * @param parent
  7. * @return 返回需要显示的View
  8. */
  9. @Override
  10. public View getView(int position, View convertView, ViewGroup parent) {
  11. TextView textView = null;
  12. if (convertView == null) {
  13. System.out.println("创建新对象");
  14. textView = new TextView(MainActivity.this);
  15. } else {
  16. System.out.println("复用老对象");
  17. textView = (TextView) convertView;
  18. }
  19. textView.setText("这是第" + position + "个TextView");
  20. return textView;
  21. }

结果如下:

 

原文链接:http://www.cnblogs.com/Dylansuns/p/14111286.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号