Images were Shuffled when scrolling a ListView with a ViewHolder

前端 未结 2 1086
情歌与酒
情歌与酒 2021-01-02 11:53

My problem is connected when the user scrolls the ListView. I looked around and saw numerous examples of \'listview lazy image\', has also watched the video of the Google IO

相关标签:
2条回答
  • 2021-01-02 12:39

    Used as base the example on the blog of Android.

    http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

    public View getView(int position, View convertView, ViewGroup parent) {
    
        ViewHolder viewHolder = new ViewHolder();
        if(convertView == null){
            convertView = _inflate.inflate(R.layout.layout_list, null);
            viewHolder.text = (TextView) convertView.findViewById(R.id.title);
            viewHolder.owner = (TextView) convertView.findViewById(R.id.owner);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.thumb);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
    
        HashMap<String, String> item = (HashMap<String, String>) getItem(position);
    
        viewHolder.text.setText( item.get("poiName").toString() );
        viewHolder.owner.setText( item.get("owner").toString() );
        viewHolder.image.setTag(item.get("thumbs"));
        imageDowload.download(item.get("thumbs"), viewHolder.image);
    
    
        return convertView;
    }
    

    is now working, thanks.

    0 讨论(0)
  • 2021-01-02 12:40

    Your problem is that you are colling notifyDataSetChange() inside of getView method

    if(!item.get("thumbs").equals("null")){
        Drawable cacheImage = loader.loadDrawable(item.get("thumbs"), new ImageManage.ImageCallback() {
            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                ImageView imageViewByTag = (ImageView) _listView.findViewWithTag(imageUrl);
                if(imageViewByTag != null)
                    imageViewByTag.setBackgroundDrawable(imageDrawable);
            }
        });
        imageView.setImageDrawable(cacheImage);
        notifyDataSetChanged();
    }
    

    code above should be done outside of getView method.

    0 讨论(0)
提交回复
热议问题