Android用volley + recyclerView加载图片

时间:2016-04-13 22:36:39

标签: android android-recyclerview android-volley

我试图在RecyclerView中加载图片。

它工作正常,直到我向左和向右滚动(水平RecyclerView),一些图片消失(边缘处的图片),并出现以下错误:

  

Volley:[3267] NetworkDispatcher.run:未处理的异常java.lang.IllegalArgumentException:width和height必须是> 0

现在这是我的适配器类(我相信问题在这里):

package com.rezadiscount.rezadiscount.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.android.volley.toolbox.NetworkImageView;
import com.rezadiscount.rezadiscount.R;
import com.rezadiscount.rezadiscount.activities.FragmentBusinessProfile;
import com.rezadiscount.rezadiscount.utilities.HTTPVolleyRequest;
import com.rezadiscount.rezadiscount.utilities.QuickstartPreferences;

import java.util.ArrayList;

//TODO Images disappear in list

/**
 * Adapter of finding a business by category
 */
public class BusinessPictureAdapter extends RecyclerView.Adapter<BusinessPictureAdapter.CategoryViewHolder> {
    private ArrayList<String> businessPictureList;
    private Context con;

    // Provide a suitable constructor (depends on the kind of dataset)
    public BusinessPictureAdapter(ArrayList<String> businessPictureListP, Context conP) {
        con = conP;
        businessPictureList = businessPictureListP;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public BusinessPictureAdapter.CategoryViewHolder onCreateViewHolder(ViewGroup parent,
                                                                        int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.business_picture_item, parent, false);
        // set the view's size, margins, paddings and layout parameters

        return new CategoryViewHolder((LinearLayout) v);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(CategoryViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.imageView.setImageUrl(QuickstartPreferences.URL_IMAGES + businessPictureList.get(position), HTTPVolleyRequest.getInstance(con).getImageLoader());

        Log.d("CardView Text", "Image url" + QuickstartPreferences.URL_IMAGES + businessPictureList.get(position));

        //holder.currentItem = items.get(position);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return businessPictureList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public class CategoryViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public NetworkImageView imageView;

        public CategoryViewHolder(LinearLayout v) {
            super(v);
            imageView = (NetworkImageView) v.findViewById(R.id.business_picture);

            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }
}

注意:我已经用Google搜索了它,到目前为止还无法解决问题。任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

好吧,正如Tranhieu建议的那样,我使用毕加索,它现在有效。它使用起来非常简单,非常性感,我推荐它。现在为什么排球可能不是缓存的不同管理,我不会看得更远。