Android ListView之EfficientAdapte的使用详解

发布时间 - 2026-01-11 02:35:58    点击率:

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用法中与滚动相关的需求实现  # 官网  # 的是  # 好了  # 一句  # 如有  # 也能  # 网上  # 请大家  # 希望能  # 刚开始  # 为例  # 所示  # 到过  # 谢谢大家  # 件事  # 就以  # 能满足  # 是一个非常  # 如何使用  # 先看 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何处理和验证JSON类型的数据库字段  使用PHP下载CSS文件中的所有图片【几行代码即可实现】  微信小程序 require机制详解及实例代码  如何在景安服务器上快速搭建个人网站?  Android Socket接口实现即时通讯实例代码  PHP 实现电台节目表的智能时间匹配与今日/明日轮播逻辑  Android滚轮选择时间控件使用详解  Python文件流缓冲机制_IO性能解析【教程】  Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道  Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】  如何在万网利用已有域名快速建站?  如何破解联通资金短缺导致的基站建设难题?  Win11怎样安装网易有道词典_Win11安装词典教程【步骤】  如何批量查询域名的建站时间记录?  今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】  php打包exe后无法访问网络共享_共享权限设置方法【教程】  免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?  如何用wdcp快速搭建高效网站?  北京网站制作公司哪家好一点,北京租房网站有哪些?  Laravel如何使用Contracts(契约)进行编程_Laravel契约接口与依赖反转  微信小程序制作网站有哪些,微信小程序需要做网站吗?  Laravel如何使用Vite进行前端资源打包?(配置示例)  Laravel如何创建自定义中间件?(Middleware代码示例)  如何快速选择适合个人网站的云服务器配置?  网站制作软件免费下载安装,有哪些免费下载的软件网站?  Laravel如何实现全文搜索功能?(Scout和Algolia示例)  Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)  Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置  Python进程池调度策略_任务分发说明【指导】  如何解决hover在ie6中的兼容性问题  如何快速搭建高效可靠的建站解决方案?  php 三元运算符实例详细介绍  详解Android图表 MPAndroidChart折线图  香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧  进行网站优化必须要坚持的四大原则  Python制作简易注册登录系统  百度浏览器如何管理插件 百度浏览器插件管理方法  EditPlus 正则表达式 实战(3)  Laravel如何实现事件和监听器?(Event & Listener实战)  Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  Laravel Session怎么存储_Laravel Session驱动配置详解  Laravel怎么自定义错误页面_Laravel修改404和500页面模板  如何快速搭建自助建站会员专属系统?  如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?  javascript如何操作浏览器历史记录_怎样实现无刷新导航  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  昵图网官网入口 昵图网素材平台官方入口  如何在阿里云高效完成企业建站全流程?  实例解析Array和String方法