Android使用Volley + RecyclerView加载图像 [英] Android loading image with volley + recyclerView
问题描述
我正在尝试在RecyclerView
中加载图像.
I'm trying to load images in a RecyclerView
.
一切正常,直到我左右滚动(水平RecyclerView
),一些图片消失(边缘的图片),并出现以下错误:
It's working alright until I scroll left and right (horizontal RecyclerView
), some pictures disappear (the one at the edges) with the following error :
齐射:[3267] NetworkDispatcher.run:未处理的异常java.lang.IllegalArgumentException:宽度和高度必须> 0
Volley: [3267] NetworkDispatcher.run: Unhandled exception java.lang.IllegalArgumentException: width and height must be > 0
现在这是我的适配器类(我相信问题在这里):
Now here is my adapter class (I believe the problem is here) :
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) {
}
});
}
}
}
注意:我已经对其进行了谷歌搜索,但到目前为止仍无法解决问题.任何帮助都将受到高度赞赏.
Note : I've googled it and couldn't solve the problem so far. Any help is highly appreciated.
推荐答案
正如Tranhieu所建议的那样,我使用了毕加索(Picasso),现在可以使用了.我推荐它,它非常简单易用,而且非常性感.现在,为什么为什么volley可能不是缓存的另一种管理方式,我将不再赘述.
Alright as Tranhieu suggested, I used Picasso and it now works. It's pretty simple to use and quite sexy, I recommend it. Now why volley didn't might be a different management of the cache, I won't look further.
这篇关于Android使用Volley + RecyclerView加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!