加上现有的ListView多个项目时ImageView的重叠 [英] ImageView overlap when adding more items in existing ListVIew

查看:627
本文介绍了加上现有的ListView多个项目时ImageView的重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个的ListView 这是一个的TextView 的ImageView 作为列表项的行。

I am creating a ListView which as a TextView and ImageView as a list item row.

起初我加载从本地数据库默认项在ListView观点,我有一个更新按钮在列表视图顶部加载来自服务器的多个项目

initially i am loading default items from local database in a listview view and I have a update button on the top of listview to load more items from server

当用户preSS的更新按钮我正在烧制的AsyncTask 这拉的图标的URL和文本从服务器。

when a user press an update button i am firing a AsyncTask which pull the icon urls and text from the server.

要加载我使用 ImageDownloader 但问题的样品ImageView的图标是我的ImageView是越来越重叠老ImageViews bcoz ViewHolder格局。所以有人POIT我什么我做错了?

to load an icon in a ImageView i am using sample of ImageDownloader but the issues is my ImageView is getting overlapped with the old ImageViews bcoz of ViewHolder pattern. so can someone poit me what am i doing wrong ?

和这里是我的ListView适配器code

and here is my ListView Adapter code

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

                ViewHolder holder;
                TemplateData data = (TemplateData) this.getItem( position );

                if(convertView == null){
                    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView=inflater.inflate(R.layout.text_template_default_row, parent, false);

                    holder = new ViewHolder();
                    holder.templateText = (TextView) convertView.findViewById(R.id.defText);
                    holder.templateIcon = (ImageView)convertView.findViewById(R.id.defIcon);
                    holder.templateTitle = (TextView) convertView.findViewById(R.id.defTitle);

                    convertView.setTag(holder);

                }else{

                    holder = (ViewHolder)convertView.getTag();

                }


                holder.templateText.setText(data.getText() );
                holder.templateTitle.setText(data.getTemplateTitle());

                //isImageLoading initially sets to false so that default items will use the 
               // resource ids , it gets falsed when AsyncTask finished load Images and update the 
               //adapter and at that time this adapter has to pic the image from ImageDowloader
                if(!isImageLoading)
                    data.setTemplateIconId(iconList[position]);


                //Has resource id but not icon url
                if(data.getTemplateIconId()!=0 && data.getTemplateIconUrl()==null ){

                    Log.d("Load icon ","Default Load");

                    holder.templateIcon.setBackgroundResource(data.getTemplateIconId());



                // does not has recource id so load url from server
                }else if(data.getTemplateIconUrl()!=null && data.getTemplateIconId()==0){


                    Log.d("Load icon ","From Server Load");

                    imageDownloader.download(data.getTemplateIconUrl(), (ImageView) holder.templateIcon);



                }




                    return convertView;

         }

iconList包含在应用程序图标,现有的资源的ID。
请随时问,如果有人想了解更多信息。

iconList contains the resources ids of existing icons in an application. Please feel free to ask if someone wants more information.

修改

下面是屏幕截图

最初将有8个模板&放大器;它的图标这是从存储在只有android手机数据库加载。从模板1的名字开始到模板6

Initially there will be 8 templates & its icon which are loading from database stored in a android phone only. its name start from template 1 to template 6

现在,当用户presses的更新按钮新模板将在这里加载。它的名字从模板中新1开始模板创建新9,但是当我滚动N个向下imageViews被重叠

now when a user presses update button new templates will be loaded over here. its name starts from template new 1 to template new 9 but the imageViews gets overlapped when i scroll up n down

这里是屏幕截图

推荐答案

我怀疑你的 imageDownloader 呼吁 setImageResource (或等同学历 - 它设置的src 属性)的的ImageView ,而你最初叫<$ C的$ C> setBackgroundResource 。这可以解释的重叠。

I suspect that your imageDownloader is calling setImageResource (or equivalent--it's setting the src attribute) of your ImageView, while you are initially calling setBackgroundResource. That would explain the overlap.

您需要做的是在以下code修改 setBackgroundResource setImageResource

What you need to do is change setBackgroundResource to setImageResource in the following code:

 if(data.getTemplateIconId()!=0 && data.getTemplateIconUrl()==null ){

                Log.d("Load icon ","Default Load");

                // This line should say setImageResource:
                holder.templateIcon.setBackgroundResource(data.getTemplateIconId());
 } else ... 

如果下载需要很长的时间,认为已经被重用的@Akos提到(他似乎已删除)该问题将是你的一个问题。重申他说,一旦你通过解决这方面的工作上面,你会发现,如果一个图像的下载需要很长的时间(只要该行已经被重用,以及新的图像集),您的图片可能覆盖与旧的图像。

The issue that @Akos mentions (which he appears to have deleted) will be an issue for you if a download takes a long time and the view has already been reused. To restate what he stated, once you get this working via the solution above, you will find that if an image download takes a long time (so long that the row has already been reused, and the new image set) that your images may be overwritten with older images.

因此​​,在 imageDownloader 你还需要下载之前说,:

Therefore, inside of imageDownloader you will also want to say, before downloading:

imageView.setTag(url);

,然后在下载完成后,在设定图像之前的的ImageView

if(!(String)imageView.getTag().equals(url)
{ 
     return; 
}

这样,如果的ImageView 在此期间得到了重用由另一行,下载将只是中止。

This way, if the ImageView has been reused by another row in the meantime, the download will simply abort.

这篇关于加上现有的ListView多个项目时ImageView的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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