stub_image 在 recyclerview 的通用图像加载器上显示
stub_image showing on universal image loader in recyclerview
我已经实现了用于在 recyclerview 中显示图像的通用图像加载器。
它显示 stub_image。
代码包含一个片段及其适配器,在我添加 universal-image-loader 之前它可以正常工作。
代码如下:
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.
引用自 Google 博文 Article。
Mipmap 文件夹基本上仅用于您的应用程序图标 (ic_launcher.png),任何其他可绘制文件(如选择器、形状可绘制对象、其他图标等)应放在相应的可绘制文件夹中。
我已经实现了用于在 recyclerview 中显示图像的通用图像加载器。 它显示 stub_image。 代码包含一个片段及其适配器,在我添加 universal-image-loader 之前它可以正常工作。
代码如下:
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.
引用自 Google 博文 Article。
Mipmap 文件夹基本上仅用于您的应用程序图标 (ic_launcher.png),任何其他可绘制文件(如选择器、形状可绘制对象、其他图标等)应放在相应的可绘制文件夹中。