在getView再循环convertView为什么我们应该重新赋值() [英] Why we should re-assign values for a recycled convertView in getView()

查看:213
本文介绍了在getView再循环convertView为什么我们应该重新赋值()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如(Xamarin C#示例,但概念是相同的Andr​​oid的Java还):
数据是保持数据的类

For example (Xamarin c# example, but concept is same for Android java also): "data" is the class which holds the data

   public override View getView (int position, View convertView, ViewGroup parent){
       if( convertView == null ){

           convertView = inflater.inflate(Resource.layout.my_list_item, parent, false);
       }
        //just for example , hence avoiding ViewHolder here
        var textView = convertView.FindViewById<TextView>(Resource.Id.textView2);
        var imageView = convertView.FindViewById<ImageView>(Resource.Id.feedImageView);

       text.Text = data.getText(position);
       imageView = GetImageAsynch(data.getURL(position));

       return convertView;
   }

下面列表中的每一行,既TextView的和ImageView的添加/刷新,这是很好的开始,但是当用户滚动,当节目开始使用回收的意见,为什么我们需要重新分配值(文字和图像)再次,如果它已经是present在回收的意见,那将是一个在头上吧?如果我做如下:

Here for every rows in the list, both textView and imageView is added/refreshed, which is fine initially, however when user scrolling and when program start using the recycled views, why do we need to re-assign the values (text and image) again if it is already present in the recycled views, that would be an over head right? What if I do as below:

   public override View getView (int position, View convertView, ViewGroup parent){
       if( convertView == null ){

           convertView = inflater.inflate(R.layout.my_list_item, parent, false);
       }
        //just for example , hence avoiding ViewHolder here
        var textView = convertView.FindViewById<TextView>(Resource.Id.textView2);
        var imageView = convertView.FindViewById<ImageView>(Resource.Id.feedImageView);

       //this means if (recycled) view is empty, then only update the view
       if(text.Text == "")
       {
         text.Text = data.getText(position);
         imageView = GetImageAsynch(data.getURL(position));
       }

       return convertView;
   }

这是为我工作很好,但我关心的是我没有看到很多例子我在这里看到,或在互联网上的任何这样的模式。

This is working fine for me, but my concern is I didn't see any such pattern in many examples I see here or in the internet.

感谢您

更新:新增的数据类中对问题更清晰。

Update: Added "data" class to have more clarity on the question

推荐答案

在查看被回收,它不一定对的相同的的适配器项目(事实​​上,大部分的时间,这不是 - 最常见的例子是项云的观点在外面当你向下滚动,重新插入底部顶部)。这就是为什么值,需要重新分配,因为它们几乎总是依赖于位置,该不会是一样使用该视图中的最后一次的值。

When a View is recycled, it's not necessarily for the same item of the Adapter (indeed, most of the time, it's not -- the most common example is "item goes outside the view at the top as you scroll down, is reinserted at the bottom"). That's why the values need to be reassigned, because they almost always depend on the value of position, which will not be the same as the last time this view was used.

您例子是有点微不足道,只要图像的文本总是相同的,但这不是在适配器次最用途的情况

Your example is a bit trivial insofar as the text of the image are always the same, but this is not the case in most usages of Adapter views.

这篇关于在getView再循环convertView为什么我们应该重新赋值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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