gpt4 book ai didi

android - 关于ListView中ViewHolder模式实现优化

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:46 24 4
gpt4 key购买 nike

所以众所周知的 ViewHolder 模式使用通常看起来像 (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 类通常是这样的:

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

我的问题是关于 ViewHolder 的实现。
1) 为什么不使用构造函数而不是初始化每个字段?
2)为什么它使用默认访问类型而不是 protected (实际上它必须是私有(private)的,但是由于 JIT 创建的静态访问器,这会影响性能)?好吧,我猜它只是关于继承。
那么为什么以下模式不是更好(不包括“ protected 与默认”访问类型):

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 的唯一变化是:

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

无论如何它必须调用构造函数。这只是品味问题吗?或者这个版本是否以某种方式变慢了,或者它是否以某种方式影响了性能?

最佳答案

我使用的方法与您的非常相似,但我更进一步,因为 ViewHolder 是适配器类私有(private)的,我通过将 View 传递给它的构造函数来将它与类紧密耦合并在那里设置值,例如

    private class ViewHolder
{
protected final ImageView image;
protected final TextView title;
protected final TextView status;

public ViewHolder( final View root )
{
image = (ImageView) root.findViewById( R.id.artist_image );
title = (TextView) root.findViewById( R.id.artist_title );
status = (TextView) root.findViewById( R.id.artist_status );
}
}

并且在 getView(...)

 View row = convertView;

if ( null == row || null == row.getTag() )
{
row = inflater.inflate( R.layout.adapter_artists, null );
holder = new ViewHolder( row );
row.setTag( holder );
}
else
{
holder = (ViewHolder) row.getTag();
}

我喜欢这样做,因为它使我在 getView(...) 中的适配器代码更简单,并且具有 final 变量的优势。我可能会稍微提高速度使其受到保护,但我发现即使列表很大,性能也足够了。

关于android - 关于ListView中ViewHolder模式实现优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16032164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com