【问题标题】:Refresh listView Items when a picture is loaded加载图片时刷新listView Items
【发布时间】:2015-11-19 13:34:45
【问题描述】:

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

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;

有人知道我怎样才能更好地使滚动更流畅吗?提前谢谢....

【问题讨论】:

  • getView 长什么样子?
  • 平滑滚动通常受您如何设计列表项的布局和适配器的 getView() 方法的影响。您可能想发布我提到的两个代码。
  • 我已经更新了我的帖子! :)
  • 我试图在不设置位图的情况下执行我的异步调用并且它再次平滑,所以问题真的来自每次我得到一个新位图时调用的 dataSetChanged

标签: android listview asynchronous bitmap notifydatasetchanged


【解决方案1】:

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

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

【讨论】:

  • 谢谢我已经尝试过了,我把那个方法改成了我的问题中发布的那个
猜你喜欢
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 2013-04-11
相关资源
最近更新 更多