Android ListView之EfficientAdapte的使用详解

在做Android手机应用开发时, ListView是一个非常常用的控件。如何更新的使用它呢?其实SDK中的例子已经非常的完整了,并且能满足大多数的需要。
如果大家刚开始学习ListView,我建议大家还是直接先看官方的例子好了,这样大家会学到更好的写法以及养成更好的习惯。
下面就以EfficientAdapter为例,看看官网例子是如何使用ListView的:
请大家格外注意getView的书写方法,大家可能从网上也能找到过一些其它的例子,但是网上的写法和官网不同,建议大家采用官网例子的写法。
简要说明:要实现高效的Adapter,需要做两件事:
1. 重用getView()中的convertView,避免在不必要的时候inflating View。
2. 使用ViewHolder模式,避免在不必要的时候调用findViewById()。
顺便再提一句:若继承的是ListActivity,如果在layout xml里定义了ListView,那么该ListView的ID必须是"@id/android:list",最好再包含一个ID是"@id/android:empty"的TextView,供ListView中没有数据时,显示提示文字用。如下所示:
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
官网EfficientAdapter例子如下:
Java代码
/**
* Demonstrates how to write an efficient list adapter. The adapter used in this example binds
* to an ImageView and to a TextView for each row in the list.
*
* To work efficiently the adapter implemented here uses two techniques:
* - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
* - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
*
* The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
* getView(). This data structures contains references to the views we want to bind data to, thus
* avoiding calls to findViewById() every time getView() is invoked.
*/
public class List14 extends ListActivity {
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound to the rows.
mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return DATA.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}
private static final String[] DATA = {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam"};
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持,如有疑问请留言或者到本站社区交流讨论,大家共同进步!
# ListView
# EfficientAdapte
# Android
# ListView之
# EfficientAdapte实例详解
# Android SQLite事务处理结合Listview列表显示功能示例
# Android 实现ListView的点击变色的实例
# Android Adapter里面嵌套ListView实例详解
# Android控件ListView使用方法详解
# Android使用ListView实现滚轮的动画效果实例
# Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法
# ListView用法中与滚动相关的需求实现
# 官网
# 的是
# 好了
# 一句
# 如有
# 也能
# 网上
# 请大家
# 希望能
# 刚开始
# 为例
# 所示
# 到过
# 谢谢大家
# 件事
# 就以
# 能满足
# 是一个非常
# 如何使用
# 先看
相关文章:
完全自定义免费建站平台:主题模板在线生成一站式服务
如何将凡科建站内容保存为本地文件?
招贴海报怎么做,什么是海报招贴?
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
网站好制作吗知乎,网站开发好学吗?有什么技巧?
建站之星会员如何解锁更多建站功能?
建站之星伪静态规则如何设置?
简单实现Android文件上传
如何快速搭建虚拟主机网站?新手必看指南
如何在香港服务器上快速搭建免备案网站?
Python lxml的etree和ElementTree有什么区别
如何在阿里云购买域名并搭建网站?
如何快速配置高效服务器建站软件?
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
如何通过西部数码建站助手快速创建专业网站?
香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南
再谈Python中的字符串与字符编码(推荐)
婚礼视频制作网站,学习*后期制作的网站有哪些?
如何快速搭建二级域名独立网站?
青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?
建站之星如何快速生成多端适配网站?
陕西网站制作公司有哪些,陕西凌云电器有限公司官网?
网站建设制作、微信公众号,公明人民医院怎么在网上预约?
建站之星安装后如何配置SEO及设计样式?
如何选择最佳自助建站系统?快速指南解析优劣
建站主机类型有哪些?如何正确选型
深圳网站制作的公司有哪些,dido官方网站?
官网网站制作腾讯审核要多久,联想路由器newifi官网
网站制作和推广的区别,想自己建立一个网站做推广,有什么快捷方法马上做好一个网站?
如何在IIS中新建站点并配置端口与IP地址?
宝塔建站后网页无法访问如何解决?
如何在服务器上三步完成建站并提升流量?
大型企业网站制作流程,做网站需要注册公司吗?
开封网站制作公司,网络用语开封是什么意思?
如何快速重置建站主机并恢复默认配置?
如何打造高效商业网站?建站目的决定转化率
,在苏州找工作,上哪个网站比较好?
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
学校免费自助建站系统:智能生成+拖拽设计+多端适配
高端建站三要素:定制模板、企业官网与响应式设计优化
c# Task.ConfigureAwait(true) 在什么场景下是必须的
建站org新手必看:2024最新搭建流程与模板选择技巧
网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?
高防服务器:AI智能防御DDoS攻击与数据安全保障
专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
昆明网站制作哪家好,昆明公租房申请网上登录入口?
建站之星2.7模板:企业网站建设与h5定制设计专题
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
如何通过商城免费建站系统源码自定义网站主题?
*请认真填写需求信息,我们会在24小时内与您取得联系。