关于ListView中ViewHolder模式实现优化 [英] About ViewHolder pattern implementation optimisation in ListView

查看:127
本文介绍了关于ListView中ViewHolder模式实现优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,使用通常众所周知的ViewHolder模式如下(ListAdapter):

So the well-known ViewHolder pattern using usually looks like (ListAdapter):

    ...

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

        final Album album = albums.get(position);

        ViewHolder viewHolder = null;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.albums_list_item, null);

            final ImageView albumImage = (ImageView) convertView.findViewById(R.id.album_icon);

            final TextView txtTitle = (TextView) convertView.findViewById(R.id.album_title);

            final TextView txtDescription = (TextView) convertView.findViewById(R.id.album_copyright);

            viewHolder = new ViewHolder();
            viewHolder.albumImage = albumImage;
            viewHolder.txtTitle = txtTitle;
            viewHolder.txtDescription = txtDescription;
            convertView.setTag(viewHolder);
        }
        else
            viewHolder = (ViewHolder)convertView.getTag();

        viewHolder.txtTitle.setText(album.getTitle(locale));
        viewHolder.txtDescription.setText(album.getCopyrightInfo(locale));
        ...
        return convertView;
    }

而ViewHolder类通常是这样的:

while the ViewHolder class is usually looks like:

static class ViewHolder{
    public ImageView previewImage;
    public TextView txtTitle;
    public TextView txtDescription;
}

我的问题是关于ViewHolder实现。
1)它为什么不能使用,而不是初始化每一个领域的构造?
2)为什么会使用默认接入类型,而不是受保护的(实际上它是私有的,但这种影响的表现,由于BYT JIT的静态访问)?好吧,我只能猜测其关于继承。
那么,为什么下面的模式是不是更好(不包括保护VS默认访问类型):

My questions is about ViewHolder implementation.
1) Why doesn't it use a constructor instead of initializing every single field?
2) Why does it use the default access type instead of protected (actually it has to be private but this impacts performance due to static accessors created byt JIT)? Well, I guess its about inheritance only.
So why the following pattern is not better (excluding "protected vs default" access type):

protected static class ViewHolder{
    public final ImageView previewImage;
    public final TextView txtTitle;
    public final TextView txtDescription;

    public ViewHolder (final ImageView previewImage,  final TextView txtTitle, final TextView txtDescription){
        this.previewImage = previewImage;
        this.txtTitle = txtTitle;
        this.txtDescription = txtDescription;
    }
}

和在ListAdapter唯一的变化是:

and the only change in ListAdapter is:

...
final TextView txtDescription = (TextView) convertView.findViewById(R.id.album_copyright);
viewHolder = new ViewHolder(albumImage, txtTitle, txtDescription);
convertView.setTag(viewHolder);
...

反正它必须调用构造函数。是它的味道只是一个问题?还是这版本速度较慢好歹它影响在某些方面的表现?

Anyway it must call a constructor. Is it just a matter of taste? Or is this version slower somehow or does it impact performance in some way?

推荐答案

我认为,这是一种个人品味只是母校。而对于我来说,它甚至看起来更好,然后标准之一。此外,您的版本可能会利用最后的变数,因为有可能更快。

I thinks that it is just mater of taste. As for me it is even looks better then standard one. Also your version probably will be potentially faster because of using final variables.

这篇关于关于ListView中ViewHolder模式实现优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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