我有Recyclerview
GridLayoutManager
。 Recyclerview
中的列表项会从包含JSON
和Name
的网址中加载简单Image URL
。我已按照this教程加载了收到的网址中的asynchronously
图片。当我的应用第一次加载时,这就是我所看到的。
这是Adapter
Recyclerview
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesViewHolder> {
private LayoutInflater inflater;
private CategoriesViewHolder viewHolder;
private ArrayList<Categories> categoriesList = new ArrayList<>();
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
public CategoriesAdapter(Context context){
inflater = LayoutInflater.from(context);
volleySingleton = VolleySingleton.getsInstance();
imageLoader = volleySingleton.getImageLoader();
}
public void setCategoriesList(ArrayList<Categories> categoriesList){
this.categoriesList = categoriesList;
notifyItemRangeChanged(0, categoriesList.size());
}
@Override
public CategoriesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.categories_list, parent, false);
viewHolder = new CategoriesViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CategoriesViewHolder holder, int position) {
Categories current = categoriesList.get(position);
viewHolder.categoryName.setText(current.categoryName);
imageLoader.get(current.icon_url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
viewHolder.categoryIcon.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
//viewHolder.contactIcon.setImageResource(R.drawable.image);
}
});
}
@Override
public int getItemCount() {
return categoriesList.size();
}
}
这是我的VolleySingleton
班级
public class VolleySingleton {
private static VolleySingleton sInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader imageLoader;
private VolleySingleton(){
mRequestQueue = Volley.newRequestQueue(MyApplication.getsInstance());
imageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<>((int) (Runtime.getRuntime().maxMemory()/1024)/8);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static VolleySingleton getsInstance(){
if(sInstance==null){
sInstance = new VolleySingleton();
}
return sInstance;
}
public RequestQueue getmRequestQueue(){
return mRequestQueue;
}
public ImageLoader getImageLoader(){
return imageLoader;
}
}
答案 0 :(得分:0)
我个人建议您使用此库Picasso,而不是自己完成所有繁重的工作。
加载图片就像这样简单,
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
答案 1 :(得分:0)
很难说为什么你会看到你所看到的东西,但是有些问题使得这种方法不太可用。
在onBindViewHolder
:
ImageView.setImageBitmap
BaseAdapter.notifyDataSetChanged
)。理想情况下,您只想更新已更改的视图,而RecyclerView.Adapter
确实允许通过notifyItemChanged
进行更新。两者都是&#34;通知&#34;方法将导致onBindViewHolder
被调用,这一次,第1步应该会成功。据说,使用第三方库,按照前一作者的建议(我偏向Glide)。它使事情变得相当容易,并且你不太可能在脚下射击自己 - 并且实现自定义图像加载/缓存存在许多问题。