UIL图像在listview android中反复加载

时间:2014-10-30 11:14:05

标签: android listview android-listview universal-image-loader

我有一个列表视图,其中只包含10个项目,每个项目包含5个图像。

添加这样的数据......

HomeCardListItemAdapter adapter = new HomeCardListItemAdapter(getActivity(),
            R.layout.home_cardlist_item, FriendCards);

在我的第一个活动中使用此配置初始化Image loader。

    File cacheDir = StorageUtils.getOwnCacheDirectory(this, "FriendCache");
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
    .diskCache(new UnlimitedDiscCache(cacheDir)) 
    .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
    .build();
    ImageLoader.getInstance().init(config);

在我的HomeCardListItemAdapter中,我正在显示这样的图片......在下面添加整个getview方法。

    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    HomeCardDetails rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null || position == 0) {
        convertView = mInflater.inflate(R.layout.home_cardlist_item, null);
        holder = new ViewHolder();
        holder.LLPaddingHelper = (LinearLayout) convertView.findViewById(R.id.LLPaddingHelper);
        holder.txtName = (TextView) convertView.findViewById(R.id.TVUserName);
        holder.imageView = (ImageView) convertView.findViewById(R.id.IVProfilePic);
        holder.pb = (ProgressBar) convertView.findViewById(R.id.pb);
        holder.txtInteraction1 = (TextView) convertView.findViewById(R.id.txtInteraction1);
        holder.IVInteraction1 = (ImageView) convertView.findViewById(R.id.IVInteraction1);
        holder.txtInteraction2 = (TextView) convertView.findViewById(R.id.txtInteraction2);
        holder.IVInteraction2 = (ImageView) convertView.findViewById(R.id.IVInteraction2);
        holder.txtInterest1 = (TextView) convertView.findViewById(R.id.txtInterest1);
        holder.IVInterest1 = (ImageView) convertView.findViewById(R.id.IVInterest1);
        holder.txtInterest2 = (TextView) convertView.findViewById(R.id.txtInterest2);
        holder.IVInterest2 = (ImageView) convertView.findViewById(R.id.IVInterest2);
        holder.RLProfileLink = (RelativeLayout) convertView.findViewById(R.id.RLProfileLink);


        convertView.setTag(holder);
    } else{
        holder = (ViewHolder) convertView.getTag();
    }

    if(position == 0)
    {
    }else{
        holder.LLPaddingHelper.setVisibility(View.GONE);
    }
    holder.txtName.setText(rowItem.Name);
    display(holder.imageView, rowItem.photoUrl.concat("?type=small"), holder.pb);

    if(rowItem.Interactions.size() >= 2){
        holder.txtInteraction1.setText(rowItem.Interactions.get(0).Name);
        display(holder.IVInteraction1, rowItem.Interactions.get(0).photoUrl);
        holder.txtInteraction2.setText(rowItem.Interactions.get(1).Name);
        display(holder.IVInteraction2, rowItem.Interactions.get(1).photoUrl);
    }

    if(rowItem.Interests.size() >= 2){
        holder.txtInterest1.setText(rowItem.Interests.get(0).Name);
        display(holder.IVInterest1, rowItem.Interests.get(0).photoUrl);
        holder.txtInterest2.setText(rowItem.Interests.get(1).Name);
        display(holder.IVInterest2, rowItem.Interests.get(1).photoUrl);
    }

    return convertView;

}   

这些是我的显示功能。

    public void display(ImageView img, String url, final ProgressBar spinner){

    imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
        boolean cacheFound;

        @Override
        public void onLoadingStarted(String imageUri, View view) {
            spinner.setVisibility(View.VISIBLE);
            List<String> memCache = MemoryCacheUtils.findCacheKeysForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
            cacheFound = !memCache.isEmpty();
            if (!cacheFound) {
                File discCache = DiskCacheUtils.findInCache(imageUri, ImageLoader.getInstance().getDiskCache());
                if (discCache != null) {
                    cacheFound = discCache.exists();
                }
            }
        }
        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            spinner.setVisibility(View.GONE);
        }
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            spinner.setVisibility(View.GONE);
            if (cacheFound) {
                MemoryCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getMemoryCache());
                DiskCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getDiskCache());

                ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);
            }
        }
        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }

    });
}

public void display(ImageView img, String url)
{
    imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
        boolean cacheFound;

        @Override
        public void onLoadingStarted(String imageUri, View view) {
            List<String> memCache = MemoryCacheUtils.findCacheKeysForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
            cacheFound = !memCache.isEmpty();
            if (!cacheFound) {
                File discCache = DiskCacheUtils.findInCache(imageUri, ImageLoader.getInstance().getDiskCache());
                if (discCache != null) {
                    cacheFound = discCache.exists();
                }
            }
            }
        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

        }
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (cacheFound) {
                MemoryCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getMemoryCache());
                DiskCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getDiskCache());

                ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);
            }
        }
        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }

});
}

这是我的适配器中的DisplayImage选项

imageLoader = ImageLoader.getInstance();
    options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.image_loading_logo)
        .cacheInMemory(true)
        .cacheOnDisk(true)
        .displayer(new RoundedBitmapDisplayer(5))
        .build();

我的问题是,当我等待第一个项目直到加载所有图像时,然后转到最后一个项目(意味着第10个项目)并返回到第一个项目,其所有图像再次加载。这个问题不仅出现在所有项目中的第一项。

0 个答案:

没有答案