ViewHolder的标准写法

最标准的写法,就是为每一个AdapterView的子View新建一个对应的ViewHolder,同时声明为prtivate final static。ViewHolder类中定义各种成员变量。

    public final static class ViewHolder{
        ImageView iv;
        TextView tv;
        Button btn;
    }

在适配器的getView()方法中:

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {

    convertView = mInflater.inflate(R.layout.list_item, null);

    holder = new ViewHolder();

    holder.text = (TextView) convertView.findViewById(R.id.text);

    convertView.setTag(holder);

} else {

    holder = (ViewHolder) convertView.getTag();

}

holder.text.setText("line" + position);

  return convertView;

}

private class ViewHolder {

  TextView text;

}

背景介绍完毕,我们现在找到了一个等价的作法,而且代码很简洁。

ViewHolder中写了一个静态方法,用泛型提供了高扩展性。我们可以通过get方法得到各种view的子类,比如imageView,button,TextView之类的。

public class ViewHolder {
        // I added a generic return type to reduce the casting noise in client code
        @SuppressWarnings("unchecked")
        public static <T extends View> T get(View view, int id) {
            SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
            if (viewHolder == null) {
                viewHolder = new SparseArray<View>();
                view.setTag(viewHolder);
            }
            View childView = viewHolder.get(id);
            if (childView == null) {
                childView = view.findViewById(id);
                viewHolder.put(id, childView);
            }
            return (T) childView;
        }
    }

在适配器中,我们可以进行如下配置(仅仅是举例)

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                // init convertView by layout
                convertView = LayoutInflater.from(context.inflate(R.layout.item, parent, false));
            }
            ImageView imageView = ViewHolder.get(convertView, R.id.imageView_id);
            imageView.setImageResource(R.drawable.ic_launcher);

            return convertView;
        }

这里的知识点是SparseArray<View>,SparseArray<View>在代码理解上等价于HashMap<Interger, View>,SparseArray是Android提供的一个数据结构,旨在提高查询的效率。所以View childView = viewHolder.get(id);这句代码的时间上的开销是极小的,完全不会影响到执行的效率。

参考自:

http://my.oschina.net/jack1900/blog/289164

http://blog.csdn.net/hmily7532361/article/details/18368235

时间: 2024-07-30 11:48:51

ViewHolder的标准写法的相关文章

ViewHolder模式超简洁写法,很cool!

ViewHolder是什么就不解释了.        大家通常怎么写ViewHolder呢? ViewHolder holder = null; if(convertView == null){ convertView = mInflater.inflate(R.layout.xxx null); holder = new ViewHolder(); holder.tvXXX = (TextView)findViewById(R.id.xxx); //...一连串的findViewById }

ViewHolder模式简洁写法

参考网址:http://blog.csdn.net/hmily7532361/article/details/18368235 在安卓中 自定义Adapter时,使用ViewHolder可缓存每个Item的View,减少一些不必要的操作,提高性能.在网上看到一种比较简洁的写法,避免每个自定义的Adapter定义ViewHolder实体类. import android.util.SparseArray; import android.view.View; /** * * @类名 ViewHold

android中ViewHolder通用简洁写法

public class ViewHolder {     // I added a generic return type to reduce the casting noise in client code     @SuppressWarnings("unchecked")     public static <T extends View> T get(View view, int id) {         SparseArray<View> view

$.ajax()参数详解及标准写法(转)

1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和delete也可以使用,但仅部分浏览器支持. 3.timeout: 要求为Number类型的参数,设置请求超时时间(毫秒).此设置将覆盖$.ajaxSetup()方法的全局设置. 4.async: 要求为Boolean类型的参数,默认设置为true,所有请求均为异步请求.如果需要发送同步请求

html标记语言的标准写法-参考自http://www.zhihu.com/question/20797118/answer/16212312

网页头部的声明应该是用 lang="zh" 还是 lang="zh-cn"? 添加评论 查看全部 12 个回答 skydiver ,程序员 5 人赞同 两种写法都可以,看你的需求选择.参考 W3C: Language information and text direction: Briefly, language codes consist of a primary code and a possibly empty series of subcodes: lan

ViewHolder的改进写法

先看看ViewHolder通用写法         ViewHolder holder = null;         if(convertView == null){                 convertView = mInflater.inflate(R.layout.xxx null);                 holder = new ViewHolder();                 holder.tvXXX = (TextView)findViewById(

从认识面向对象到构造函数的标准写法

认识面向对象: /* 五点 1.发展历史 2.编程思想 面向过程的编程思想:只关心数学逻辑. 面向对象的编程思想:直接生活逻辑映射到我们的程序中. 3.语法 类 对象 类:具有一类相同特征的事物的抽象概念. 对象:具体的某一个实体,唯一的. 4.代码 5.结构 基本数据类型(单个数据) -> 数组(批量数据) -> 对象(既能够存储数据,又能够存储函数) */ 我们想创建两个对象,利用传统的对象方式: 1 <script> 2 /* 3 [注]面向对象的特点是继承.封装.多态. 4

SQL学习(二)之四大查询语句以及标准写法

SQL四大查询语句——增删改查 增-INSERT INSERT INTO 表 (字段列表) VALUES(值列表) INSERT INTO `user_table` (`ID`, `username`, `password`) VALUES(0, 'blue2', '987654'); // 给0是因为我们之前设置了自动增加,而0不是一个合法的id值,所以就会自动给我们添 删-DELETE DELETE FROM 表 删除表 DELETE FROM `user_table` 注意:没有办法只删除

数字的标准写法

#include <bits/stdc++.h> using namespace std; #define ll long long char s[20],p[20]; int cnt; void solve(ll c){ if(c<0){ printf("-"); c=abs(c); } cnt = 0; while(c){//为0呢 s[cnt++] = char(c%10)+'0'; c/=10; } for(int i =0;i<cnt/2;i++){