什么是ViewHolder的好处? [英] What is the benefit of ViewHolder?

查看:175
本文介绍了什么是ViewHolder的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你正在开发一个Android程序;你想有一个 ArrayAdapter 您可以简单地用类(大部分是 ViewHolder 后缀倍),或直接膨胀您的 convertView ,并找到你鉴于ID。
那么,什么是使用ViewHolder的好处?
都在这里的例子:

 如果(convertView == NULL)
        {
            convertView =((活动)_context).getLayoutInflater()膨胀(R.layout.row_phrase,空)。
        }
((TextView中)convertView.findViewById(R.id.txtPhrase))的setText(短语01);
 

或者

 静态类ViewHolder {
ImageView的leftIcon;
TextView的upperLabel;
TextView的lowerLabel;
}
 

终于在getView:

  ViewHolder持有人= NULL;
  如果(查看== NULL){
   鉴于= LayoutInflater.from(上下文).inflate(R.layout.row_layout,
   空,假);
   持有人=新ViewHolder();
   holder.leftIcon =(ImageView的)view.findViewById(R.id.leftIcon);
 

解决方案

了解如何ListView的回收工作。

如何ListView的回收机制的工作

您不能再利用的行为presently使用。上面的链接解释如何ListView的回收机制的工作原理

  

那么,什么是使用ViewHolder的好处?

引用文档

您code可以称之为 findViewById()频繁的ListView的滚动,这会降低性能时。即使当适配器返回回收膨胀的观点,你仍然需要查找的元素和更新。一个倒过来重复使用 findViewById的()是使用观点持有者的设计模式。

 公开查看getView(INT位置,查看convertView,ViewGroup中父){
             ViewHolder持有人;

             如果(convertView == NULL){//如果convertView为null
                 convertView = mInflater.inflate(R.layout.mylayout,
                         父母,假);
                 持有人=新ViewHolder();
                     //初始化意见
                convertView.setTag(保持器);在视图//设置标签
            } 其他 {
                支架=(ViewHolder)convertView.getTag();
                        //如果不为空GET标签
                        //无需初始化
            }

            这里//更新视图
            返回convertView;
    }
 

您错过了重要的部分 convertView.setTag(持有人)持有人=(ViewHolder)ConvertView.getTag()

http://developer.android.com/training/improving-布局/平滑scrolling.html

When you are developing an Android program; and you want to have a ArrayAdapter you can Simply have an Class (most of times with ViewHolder suffix) or directly inflate your convertView and find your view by id.
So What is the benefit of using ViewHolder?
The example of both here :

        if(convertView==null)
        {
            convertView = ((Activity)_context).getLayoutInflater().inflate(R.layout.row_phrase, null);
        }
((TextView)convertView.findViewById(R.id.txtPhrase)).setText("Phrase 01");  

Or :

static class ViewHolder {   
ImageView leftIcon;   
TextView upperLabel;  
TextView lowerLabel;  
}

and finally in the getView :

ViewHolder holder = null;
  if (view == null) {
   view = LayoutInflater.from(context).inflate(R.layout.row_layout,
   null, false);
   holder = new ViewHolder();
   holder.leftIcon = (ImageView) view.findViewById(R.id.leftIcon);

解决方案

Understand how listview recycling works

How ListView's recycling mechanism works

You cannot recycle a row that is presently in use. The above link explains how listview recycling mechanism works

So What is the benefit of using ViewHolder?

Quoting docs

Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.

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

             if (convertView == null) { // if convertView is null
                 convertView = mInflater.inflate(R.layout.mylayout, 
                         parent, false);
                 holder = new ViewHolder(); 
                     // initialize views  
                convertView.setTag(holder);  // set tag on view
            } else { 
                holder = (ViewHolder) convertView.getTag();
                        // if not null get tag 
                        // no need to initialize
            } 

            //update views here  
            return convertView; 
    }

You missed the important part convertView.setTag(holder) and holder = (ViewHolder) ConvertView.getTag()

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

这篇关于什么是ViewHolder的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆