[Android] ListView中getView的原理+如何在ListView中放置多个item

C#中ListView详解 ListView是一个Windows 列表视图控件,该控件显示可用五种不同视图之一显示项的集合 阅读详情

ListView 和 Adapter 的基础

工作原理:

  1. ListView 针对List中每个item,要求 adapter “给我一个视图” (getView)。
  2. 一个新的视图被返回并显示

如果我们有上亿个项目要显示怎么办?为每个项目创建一个新视图?NO!这不可能!

实际上Android为你缓存了视图。

Android中有个叫做Recycler的构件,下图是他的工作原理:

  1. 如果你有10亿个项目(item),其中只有可见的项目存在内存中,其他的在Recycler中。
  2. ListView先请求一个type1视图(getView)然后请求其他可见的项目。convertView在getView中是空(null)的。
  3. 当item1滚出屏幕,并且一个新的项目从屏幕低端上来时,ListView再请求一个type1视图。convertView此时不是空值了,它的值是item1。你只需设定新的数据然后返回convertView,不必重新创建一个视图。

请看下面的示例代码,这里在getView中使用了System.out进行输出

public class MultipleItemsList extends ListActivity {
  
     private MyCustomAdapter mAdapter;
  
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         mAdapter = new MyCustomAdapter();
         for ( int i = 0 ; i < 50 ; i++) {
             mAdapter.addItem( "item " + i);
         }
         setListAdapter(mAdapter);
     }
  
     private class MyCustomAdapter extends BaseAdapter {
  
         private ArrayList mData = new ArrayList();
         private LayoutInflater mInflater;
  
         public MyCustomAdapter() {
             mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         }
  
         public void addItem( final String item) {
             mData.add(item);
             notifyDataSetChanged();
         }
  
         @Override
         public int getCount() {
             return mData.size();
         }
  
         @Override
         public String getItem( int position) {
             return mData.get(position);
         }
  
         @Override
         public long getItemId( int position) {
             return position;
         }
  
         @Override
         public View getView( int position, View convertView, ViewGroup parent) {
             System.out.println( "getView " + position + " " + convertView);
             ViewHolder holder = null ;
             if (convertView == null ) {
                 convertView = mInflater.inflate(R.layout.item1, null );
                 holder = new ViewHolder();
                 holder.textView = (TextView)convertView.findViewById(R.id.text);
                 convertView.setTag(holder);
             } else {
                 holder = (ViewHolder)convertView.getTag();
             }
             holder.textView.setText(mData.get(position));
             return convertView;
         }
  
     }
  
     public static class ViewHolder {
         public TextView textView;
     }
}

执行程序,然后在Logcat中查看日志

 

getView 被调用 9 次 ,convertView 对于所有的可见项目是空值(如下)

 

?
02 - 05 13 : 47 : 32.559 : INFO/System.out( 947 ): getView 0 null
02 - 05 13 : 47 : 32.570 : INFO/System.out( 947 ): getView 1 null
02 - 05 13 : 47 : 32.589 : INFO/System.out( 947 ): getView 2 null
02 - 05 13 : 47 : 32.599 : INFO/System.out( 947 ): getView 3 null
02 - 05 13 : 47 : 32.619 : INFO/System.out( 947 ): getView 4 null
02 - 05 13 : 47 : 32.629 : INFO/System.out( 947 ): getView 5 null
02 - 05 13 : 47 : 32.708 : INFO/System.out( 947 ): getView 6 null
02 - 05 13 : 47 : 32.719 : INFO/System.out( 947 ): getView 7 null
02 - 05 13 : 47 : 32.729 : INFO/System.out( 947 ): getView 8 null

 

然后稍微向下滚动List,直到item10出现:

 

convertView仍然是空值,因为recycler中没有视图(item1的边缘仍然可见,在顶端)

 

?
02 - 05 13 : 48 : 25.169 : INFO/System.out( 947 ): getView 9 null

再滚动List

 

convertView不是空值了!item1离开屏幕到Recycler中去了,然后item11被创建

 

?
02 - 05 13 : 48 : 42.879 : INFO/System.out( 947 ): getView 10 android.widget.LinearLayout @437430f8

 

再滚动:

?
02 - 05 14 : 01 : 31.069 : INFO/System.out( 947 ): getView 11 android.widget.LinearLayout @437447d0
02 - 05 14 : 01 : 31.142 : INFO/System.out( 947 ): getView 12 android.widget.LinearLayout @43744ff8
02 - 05 14 : 01 : 31.279 : INFO/System.out( 947 ): getView 13 android.widget.LinearLayout @43743fa8
02 - 05 14 : 01 : 31.350 : INFO/System.out( 947 ): getView 14 android.widget.LinearLayout @43745820
02 - 05 14 : 01 : 31.429 : INFO/System.out( 947 ): getView 15 android.widget.LinearLayout @43746048
02 - 05 14 : 01 : 31.550 : INFO/System.out( 947 ): getView 16 android.widget.LinearLayout @43746870
02 - 05 14 : 01 : 31.669 : INFO/System.out( 947 ): getView 17 android.widget.LinearLayout @43747098
02 - 05 14 : 01 : 31.839 : INFO/System.out( 947 ): getView 18 android.widget.LinearLayout @437478c0
02 - 05 14 : 03 : 30.900 : INFO/System.out( 947 ): getView 19 android.widget.LinearLayout @43748df0
02 - 05 14 : 03 : 32.069 : INFO/System.out( 947 ): getView 20 android.widget.LinearLayout @437430f8

convertView 如我们所期待的非空了,在item11离开屏幕之后,它的视图(@437430f8)作为convertView容纳item21了

不同的项目布局(item layout)

我们再举一个稍微复杂的例子,在上例的list中加入一些分隔线

你需要做这些:

  1. 重(@Override)写 getViewTypeCount() – 返回你有多少个不同的布局
  2. 重写 getItemViewType(int) – 由position返回view type id
  3. 根据view item的类型,在getView中创建正确的convertView

以下是代码:

public class MultipleItemsList extends ListActivity {
  
     private MyCustomAdapter mAdapter;
  
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         mAdapter = new MyCustomAdapter();
         for ( int i = 1 ; i < 50 ; i++) {
             mAdapter.addItem( "item " + i);
             if (i % 4 == 0 ) {
                 mAdapter.addSeparatorItem( "separator " + i);
             }
         }
         setListAdapter(mAdapter);
     }
  
     private class MyCustomAdapter extends BaseAdapter {
  
         private static final int TYPE_ITEM = 0 ;
         private static final int TYPE_SEPARATOR = 1 ;
         private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1 ;
  
         private ArrayList mData = new ArrayList();
         private LayoutInflater mInflater;
  
         private TreeSet mSeparatorsSet = new TreeSet();
  
         public MyCustomAdapter() {
             mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         }
  
         public void addItem( final String item) {
             mData.add(item);
             notifyDataSetChanged();
         }
  
         public void addSeparatorItem( final String item) {
             mData.add(item);
             // save separator position
             mSeparatorsSet.add(mData.size() - 1 );
             notifyDataSetChanged();
         }
  
         @Override
         public int getItemViewType( int position) {
             return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
         }
  
         @Override
         public int getViewTypeCount() {
             return TYPE_MAX_COUNT;
         }
  
         @Override
         public int getCount() {
             return mData.size();
         }
  
         @Override
         public String getItem( int position) {
             return mData.get(position);
         }
  
         @Override
         public long getItemId( int position) {
             return position;
         }
  
         @Override
         public View getView( int position, View convertView, ViewGroup parent) {
             ViewHolder holder = null ;
             int type = getItemViewType(position);
             System.out.println( "getView " + position + " " + convertView + " type = " + type);
             if (convertView == null ) {
                 holder = new ViewHolder();
                 switch (type) {
                     case TYPE_ITEM:
                         convertView = mInflater.inflate(R.layout.item1, null );
                         holder.textView = (TextView)convertView.findViewById(R.id.text);
                         break ;
                     case TYPE_SEPARATOR:
                         convertView = mInflater.inflate(R.layout.item2, null );
                         holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                         break ;
                 }
                 convertView.setTag(holder);
             } else {
                 holder = (ViewHolder)convertView.getTag();
             }
             holder.textView.setText(mData.get(position));
             return convertView;
         }
  
     }
  
     public static class ViewHolder {
         public TextView textView;
     }
}

运行程序,你会看到每4个item一个分割线

看看日志,无异常,所有的convertView都是空的

 

?
02 - 05 15 : 19 : 03.080 : INFO/System.out( 1035 ): getView 0 null type = 0
02 - 05 15 : 19 : 03.112 : INFO/System.out( 1035 ): getView 1 null type = 0
02 - 05 15 : 19 : 03.130 : INFO/System.out( 1035 ): getView 2 null type = 0
02 - 05 15 : 19 : 03.141 : INFO/System.out( 1035 ): getView 3 null type = 0
02 - 05 15 : 19 : 03.160 : INFO/System.out( 1035 ): getView 4 null type = 1
02 - 05 15 : 19 : 03.170 : INFO/System.out( 1035 ): getView 5 null type = 0
02 - 05 15 : 19 : 03.180 : INFO/System.out( 1035 ): getView 6 null type = 0
02 - 05 15 : 19 : 03.190 : INFO/System.out( 1035 ): getView 7 null type = 0
02 - 05 15 : 19 : 03.210 : INFO/System.out( 1035 ): getView 8 null type = 0
02 - 05 15 : 19 : 03.210 : INFO/System.out( 1035 ): getView 9 null type = 1

滚动list:

?
02 - 05 15 : 19 : 54.160 : INFO/System.out( 1035 ): getView 10 null type = 0
02 - 05 15 : 19 : 57.440 : INFO/System.out( 1035 ): getView 11 android.widget.LinearLayout @43744528 type = 0
02 - 05 15 : 20 : 01.310 : INFO/System.out( 1035 ): getView 12 android.widget.LinearLayout @43744eb0 type = 0
02 - 05 15 : 20 : 01.880 : INFO/System.out( 1035 ): getView 13 android.widget.LinearLayout @437456d8 type = 0
02 - 05 15 : 20 : 02.869 : INFO/System.out( 1035 ): getView 14 null type = 1
02 - 05 15 : 20 : 06.489 : INFO/System.out( 1035 ): getView 15 android.widget.LinearLayout @43745f00 type = 0
02 - 05 15 : 20 : 07.749 : INFO/System.out( 1035 ): getView 16 android.widget.LinearLayout @43747170 type = 0
02 - 05 15 : 20 : 10.250 : INFO/System.out( 1035 ): getView 17 android.widget.LinearLayout @43747998 type = 0
02 - 05 15 : 20 : 11.661 : INFO/System.out( 1035 ): getView 18 android.widget.LinearLayout @437481c0 type = 0
02 - 05 15 : 20 : 13.180 : INFO/System.out( 1035 ): getView 19 android.widget.LinearLayout @437468a0 type = 1
02 - 05 15 : 20 : 16.900 : INFO/System.out( 1035 ): getView 20 android.widget.LinearLayout @437489e8 type = 0
02 - 05 15 : 20 : 25.690 : INFO/System.out( 1035 ): getView 21 android.widget.LinearLayout @4374a8d8 type = 0

convertView对于分割线是空的,直到第一个分割线可见,当其离开屏幕,视图去到Recycler并且convertView开始起作用。


C# ListView 的用法 在这个示例中,我们将向ListView控件中添加一个具有自定义列的数据。我们将创建一个新的ListView子类,覆盖方法以向控件中添加自定义列。在自定义列的列标题区域中,我们将添加一个新的按钮控件,用于在列标题被单击时触发事件。// 创建自定义列表头customColumnHeader.Text = "操作";// 添加按钮到自定义列表头中btn.Text = "新建客户";// TODO: 在这里添加新客户的代码在这个代码中,我们创建了一个名为的新类,并继承了。 阅读详情

相关推荐

STM32与MC74HC165A实现多路输入扩展方案

在嵌入式系统开发中,GPIO资源管理是核心挑战之一。移位寄存器通过串行化技术,将多个并行输入信号转换为串行数据流,显著节省微控制器引脚资源。MC74HC165A作为典型的8位并行输入/串行输出移位寄存器,配合STM32的GPIO或SPI接口,可实现高效的多路信号采集。这种硬件扩展方案特别适合工业控制、自动化监测等需要处理大量数字输入的场景。通过级联多个芯片,理论上可无限扩展输入通道,而仅需3个GPIO引脚控制。在软件层面,开发者既可采用基本的GPIO模拟时序,也能利用STM32的硬件SPI和DMA实现高速数

weixin_30629653的博客 422

ListView的item展开后完整显示

本文描述了使用ListView展示子项Item,子项里面包含多个内容,默认只显示主要信息,其他部分隐藏,在点击后展开。对于顶部或底部显示不全的Item,进行自动滑动展示完全。并对于子项中是否有按钮的两种情况,都进行了说明。

lintax的专栏 4851

用户存续期价值评估(CLV) 一

用户存续期价值评估(CLV) 1 一. 定义 ​     用户存续期价值(Customer Lifetime Value),也可称为CLTV、LCV或者LTV,是企业对未来一个客户与其的关系所带来的净收益的预测。这个预测模型可以有不同水平的复杂度和精确度,从一个直观感觉到使用复杂的预测技术。CLV可以被简单理解为客户关系的价值,也就是未来由于与客户的关系所带...

weixin_43171270的博客 8026

Android ListView中getView的原理+如何在ListView中放置多个item

http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html ListView 和 Adapter 的基础 工作原理: ListView 针对List中每个item,要求 adapter “给我一个视图” (getView)。一个新的视图被返回并显示 如果我们有上亿个项目要显示怎么办?为每个项目创建一个新视图?NO!这

壹口尘埃 2119

《炬丰科技-半导体工艺》用于半导体的RCA清洁技术

书籍:《炬丰科技-半导体工艺》 文章:用于半导体的RCA清洁技术 编号:JFKJ-21-1073 引言 RCA清洗技术是用于清洗硅晶圆等的技术,由于其高可靠性,30多年来一直被用于半导体和平板显示器(FPD)领域的清洗。其基础是以除去颗粒为目的的氨水-过氧化氢溶液组成的SC―1洗涤和以除去金属杂质为目的的盐酸-过氧化氢溶液组成的SC―2洗涤相结合的洗涤技术。SC-1洗涤的机理说明如下。首先,用过氧化氢氧化硅晶片的表面,用作为碱的氨蚀刻氧化硅,并通过剥离去除各种颗粒。此外,如图1所示,在pH 10.5附近的S

jfkj2021的博客 769

中兴SDH设备开局及业务配置

中兴SDH设备开局及业务配置 1.设备开局(S200-----S325) A.开电之后,按住截铃(bell off)开关,直至NCP告警灯变成橙色; B.此时,设备进入Download状态,设备IP为:192.192.192.11/24; 2.用交叉线(568A)将设备的QX口连接至电脑,电脑配置和设备相同网段的IP信息; 3.在电脑上测试连通性:pin...

weixin_33897722的博客 3996

Android中ListView中getView的原理&如何在ListView中放置多个item

参考至:http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html http://android.amberfog.com/?p=296 ListView 和 Adapter 的基础 工作原理: ListView 针对List中每个item,要求 adapter “给我一个视图” (getView)。

Kay的博客 722

实测对比:纳芯微NSD1624在光伏逆变器中的性能表现(含温度测试数据)

本文详细评测了纳芯微NSD1624非隔离驱动芯片在光伏逆变器中的性能表现,包括动态效率、高温稳定性及抗干扰能力。实测数据显示,该芯片在800V母线电压下效率峰值达99.03%,高温环境下结温稳定在102.7℃,且传播延迟一致性优异。与竞品相比,NSD1624在总损耗和热阻方面具有显著优势,是光伏逆变器驱动方案的理想选择。

weixin_29297127的博客 363
上一篇: Android异步加载图像小结 (含线程池,缓存方法)
下一篇: Java安全通信:HTTPS与SSL
stonenotes123
博客等级 码龄15年 30粉丝 176原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值