I have implement universal image loader for showing images in recyclerview. it showing stub_image. Code contain One fragment and its adapter, its working properly before i added universal-image-loader.
Here is the code:
Fragment.Class
public class FourthFragment extends Fragment {
private DisplayImageOptions options;
ImageLoader imageLoader;
RecyclerView rv;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
String[] str = {"one","two","three","four"};
int[] img = {R.mipmap.rio_one,R.mipmap.rio_two,R.mipmap.rio_three,R.mipmap.rio_four};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.view_pager_fourth_fragment, container, false);
try {
rv = (RecyclerView) v.findViewById(R.id.recycler_view);
rv.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
rv.setLayoutManager(mLayoutManager);
} catch (Exception e) {
e.printStackTrace();
Log.e("some error", "" + e);
}
mAdapter = new RecyclerView_Adapter(getActivity(), R.layout.view_pager_card_view,str,img);
rv.setAdapter(mAdapter);
return v;
}
RecyclerViewAdapter.class
private class RecyclerView_Adapter extends RecyclerView.Adapter<RecyclerView_Adapter.DataHolder> {
String [] result;
Activity activity;
int layoutResourceId;
int[] imgs;
public RecyclerView_Adapter(FragmentActivity activity, int layoutResourceId, String [] result, int[] img) {
this.layoutResourceId=layoutResourceId;
this.activity=activity;
this.result=result;
this.imgs=img;
}
public class DataHolder extends RecyclerView.ViewHolder{
protected TextView tv1;
protected ImageView iv1;
public DataHolder(View v){
super(v);
tv1=(TextView)v.findViewById(R.id.textview1);
iv1=(ImageView)v.findViewById(R.id.icon);
}
}
@Override
public DataHolder onCreateViewHolder(ViewGroup v, int i) {
View item = LayoutInflater.from(v.getContext()).inflate(R.layout.view_pager_card_view, v, false);
return new DataHolder(item);
}
@Override
public void onBindViewHolder(DataHolder holder, int position) {
ImageLoaderConfiguration configBuilder = new ImageLoaderConfiguration.Builder(getContext())
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(configBuilder);
options = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.stub_image) // STUB_IMAGE SHOWING
.showImageForEmptyUri(R.mipmap.image_for_empty_url)
.cacheInMemory(true)
.cacheOnDisc(true)
.build();
holder.tv1.setText(result[position]);
imageLoader.displayImage("mipmap://" + imgs[position], holder.iv1, options);
}
@Override
public int getItemCount() {
Log.e("LIST SIZE", "" + result.length);
return imgs.length;
}
}
It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density.
Referenced from Google BlogPost Article.
Mipmap folder's are basically for your app icons only (ic_launcher.png) and for any other drawable files (like selectors, shape drawables, other icons etc) should be put in respective drawable folder's.