首页 > 电脑

给ListView通过自定义适配器显示数据,需要哪些步骤

更新时间2019-01-05 05:32:09

首先:需要创建一个ListView控件,自定义适配器是为了实现自定义ListView的ListView_Item,所以我们应该在创建一个Item

然后书写自定义适配器,自定义适配器的时候需要继承BaseAdapter,然后实现它的未实现的4个方法,然后在实现自定义适配器的构造方法,其构造方法的参数为山下文对象,数据源,必须实现的4个方法主要是对数据源进行过操作,

然后考虑优化方面,我们使用了ViewHolder Pattern,用它的优点(我们来看一下来自国外网友的看法):

  Now let’s see how it works with the ViewHolder pattern.

       1.The first time it was loaded, convertView is null. We’ll have to inflate our list item layout, instantiate the ViewHolder, find the TextView via

       findViewById·() and assign it to the ViewHolder, and set the ViewHolder as tag of convertView.

      2.The second time it was loaded, convertView is not null, good! We don’t have to inflate it again. And here’s the sweet thing, we won’t have to call

     findViewById() since we can now access the TextView via its ViewHolder.

      3.The following time it was loaded, convertView is definitely not null. The findViewById() is never called again, and that makes our smooth ListView scrolling.


上一篇:c语言代码,求字符串"ABCXYZ"的长度是多少个字节

下一篇:云存储架构包含哪些内容