Android中的ViewHolder模式有什么好处? [英] What is the benefit of ViewHolder pattern in android?

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

问题描述

在开发Android程序时;并且您想要一个 ArrayAdapter ,您可以简单地拥有一个类(大多数情况下带有 ViewHolder 后缀)或直接为您的 convertView 充气并找到您的按ID查看.
那么使用ViewHolder有什么好处?
两者的示例:

When you are developing an Android program; and you want to have a ArrayAdapter you can Simply have a 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 :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.row_phrase, null);
    }
    ((TextView) convertView.findViewById(R.id.txtPhrase)).setText("Phrase 01");
}

或在ArrayAdapter中创建一个内部类,如下所示:

Or create an inner class in the ArrayAdapter as following:

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

,最后在getView中:

and finally in the getView :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    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

ListView的回收机制如何工作

您不能回收当前正在使用的行.上面的链接说明了listview回收机制的工作原理

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

那么使用ViewHolder有什么好处?

So What is the benefit of using ViewHolder?

引用文档

在滚动ListView时,您的代码可能会频繁调用findViewById(),这可能会降低性能.即使适配器返回膨胀视图以进行回收,您仍然需要查找元素并进行更新.重复使用findViewById()的一种方法是使用视图持有人"设计模式.

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; 
    }

您错过了重要部分convertView.setTag(holder)holder = (ViewHolder) ConvertView.getTag()

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

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

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