getView()的两种实现有什么区别?
谁能解释一下两种 getView() 实现之间有什么区别?
第一个只是检查 convertView
是否为 null;如果为 null,则膨胀一个新的 View 对象。然后为子View设置适当的值。
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.itemlayout, null, true);
}
ImageView image = (ImageView) convertView.findViewById(R.id.icon);
TextView text = (TextView) convertView.findViewById(R.id.name);
MyItem item = items[position];
text.setText(item.name);
if("male".equals(item.gender))
{
image.setImageResource(R.drawable.male);
}
else if("female".equals(item.gender))
{
image.setImageResource(R.drawable.female);
}
return convertView;
}
第二个是所谓的“ViewHolder”模式。许多开发者表示这种方法节省了大量的内存和CPU时间。但第一个实现还检查convertView是否存在。第一种方法不是节省一些内存吗?谁能更深入、更清楚地解释这两种实现之间的区别?非常感谢。
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View itemView = convertView;
if(itemView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
itemView = inflater.inflate(R.layout.itemlayout, null, true);
holder = new ViewHolder();
holder.image = (ImageView) itemView.findViewById(R.id.icon);
holder.text = (TextView) itemView.findViewById(R.id.name);
itemView.setTag(holder);
}
else
{
holder = (ViewHolder) itemView.getTag();
}
MyItem item = items[position];
holder.text.setText(item.name);
if("male".equals(item.gender))
{
holder.image.setImageResource(R.drawable.male);
}
else if("female".equals(item.gender))
{
holder.image.setImageResource(R.drawable.female);
}
return itemView;
}
Can anyone please explain what's the difference between the two kinds of getView() implementation?
The first one simply check if the convertView
is null; if it's null, inflate a new View object. Then set the sub-Views with proper values.
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.itemlayout, null, true);
}
ImageView image = (ImageView) convertView.findViewById(R.id.icon);
TextView text = (TextView) convertView.findViewById(R.id.name);
MyItem item = items[position];
text.setText(item.name);
if("male".equals(item.gender))
{
image.setImageResource(R.drawable.male);
}
else if("female".equals(item.gender))
{
image.setImageResource(R.drawable.female);
}
return convertView;
}
The second one is the so-called "ViewHolder" pattern. Many developers say that this method saves a lot of memory and CPU time. But the first implementation also checks the existence of the convertView. Doesn't the first method save some memory? Can anyone explain the difference between the two implementations more deeply and clearly? Thanks very much.
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View itemView = convertView;
if(itemView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
itemView = inflater.inflate(R.layout.itemlayout, null, true);
holder = new ViewHolder();
holder.image = (ImageView) itemView.findViewById(R.id.icon);
holder.text = (TextView) itemView.findViewById(R.id.name);
itemView.setTag(holder);
}
else
{
holder = (ViewHolder) itemView.getTag();
}
MyItem item = items[position];
holder.text.setText(item.name);
if("male".equals(item.gender))
{
holder.image.setImageResource(R.drawable.male);
}
else if("female".equals(item.gender))
{
holder.image.setImageResource(R.drawable.female);
}
return itemView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二种模式创建 ViewHolder 的静态实例,并在第一次加载时将其附加到视图项,然后在将来的调用中从该视图标记中检索
getView() 被非常频繁地调用,特别是当您滚动时大的元素列表,事实上,每次列表视图项在滚动上变得可见时都会调用它。
这可以防止 findViewById() 被多次无用地调用,将视图保持在静态引用上,这是节省一些资源的好模式(特别是当您需要引用列表视图项中的许多视图时)。
The second pattern creates a static instance of the ViewHolder and attach it to the view item the first time it is loaded, and then it will be retrieved from that view tag on the future calls
getView() is called very frequently, expecially when you scroll big lists of elements, in fact it is called each time a listview item becomes visible on scroll.
That prevents findViewById() to being called lots of times uselessy, keeping the views on a static reference, it is a good pattern to save some resources (expecially when you need to reference many views in your listview items).
使用 viewHolder 可以防止每次需要在屏幕上绑定视图时调用 findviewById() 。这意味着视图会膨胀,并且连接到它的资源仅初始化一次(膨胀、findById、侦听器、不会像静态标题等那样改变的内容)。因此,在处理列表时使用它可以提供更好的性能,因此始终推荐的方法。
using a viewHolder prevents findviewById() to be called each time a view needs to be bound on the screen. This means the view is inflated and the resources connected to it are initialized just once(inflation, findById, listeners , content that wont change like static titles or something). so using this gives better performance when working with lists thus the always recomended method.