加载图片时刷新listView项目

时间:2015-08-25 17:57:30

标签: android listview asynchronous bitmap notifydatasetchanged

在我的应用程序上,我有一个ListView,它由一个自定义的ArrayAdapter填充。每个listView项都包含一个ImageView。由于项目已填充,我开始下载与每个listView项目相关的每个图片。 当我得到图片位图时,我需要使用 notifyDataSetChanged 更新我的适配器的数据。我已经使用ViewHolder模式来获得listView的onScroll事件的平滑行为。但是因为每次我得到位图以更新数据集以显示图片时,滚动都不平滑.... 这是Async请求,它在成功回调时更新了整个dataSet。

PsPicturesServices services = new PsPicturesServices();

        services.downloadPicture(listUrls.get(numProductAllProduct), new OnPicturesServicesListener() {

            @Override
            public void OnResponse(Bitmap bitmap) {

                allProducts.get(numProductAllProduct).setDefaultImageBitmap(bitmap);

                ((ProduitsListAdapter) allProductsList.getAdapter()).notifyDataSetChanged();allProductsList.getAdapter()).notifyDataSetChanged();
                numProductAllProduct++;
                if(numProductAllProduct < listUrls.size())
                {
                    loadAllProductsPictures(listUrls);
                }

            }

            @Override
            public void OnError(VolleyError error) {
                // TODO Auto-generated method stub

            }
        });

allProducts 是首次填充时与适配器关联的列表,当我获得位图时,由于索引,我会相应地更新对象。然后我更新整个数据集...... getView方法:

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

    ProductViewHolder viewHolder;


    if(convertView == null)
    {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.produits_entretien_item_layout, parent, false);
        viewHolder = new ProductViewHolder();

        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.productImage);
        viewHolder.productNameTextView = (TextView) convertView.findViewById(R.id.productTitle);
        viewHolder.priceTextView = (TextView) convertView.findViewById(R.id.productPrice);

        convertView.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ProductViewHolder) convertView.getTag();
    }

    Produit produit = getItem(position);

    if((Constants.CATEGORIE_LENTILLES_PAR_MARQUE).equalsIgnoreCase(GlobalVariables.getInstance().getCurrentRecherche().getProductCategorie().getNom())
            || (Constants.CATEGORIE_LENTILLES_PAR_PERIODE).equalsIgnoreCase(GlobalVariables.getInstance().getCurrentRecherche().getProductCategorie().getNom()))
    {

        if(produit.getDefaultImageBitmap() != null && produit.getDefaultImageBitmap() != null)
        {
            viewHolder.imageView.setImageBitmap(produit.getDefaultImageBitmap());
        }

        viewHolder.productNameTextView.setText(produit.getNom());
        viewHolder.priceTextView.setText(Float.toString(produit.getPrixTtc()));

    }
 return convertView;

有人看到我怎样才能让滚动更顺畅?提前谢谢....

1 个答案:

答案 0 :(得分:0)

您可以在不调用notifyDataSetChanged()的情况下将图像加载到行中。最简单的方法是从ListView中获取图像索引的子视图,并直接在其上设置位图。粗糙的代码:

Bitmap myBitmap = ...;
int index = ...;
int childIndex = index - mListView.getFirstVisiblePosition();
ImageView view = mListView.getChildAt(childIndex).findViewById(R.id.my_image_view);
view.setImageBitmap(myBitmap);