我已经实现了通用图像加载器,用于在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;
}
}
答案 0 :(得分:1)
最佳做法是将应用程序图标放在mipmap文件夹中(而不是 drawable-文件夹)因为它们的使用分辨率不同于 设备的电流密度。
引自Google BlogPost Article。
Mipmap文件夹基本上只适用于您的应用程序图标(ic_launcher.png),任何其他可绘制文件(如选择器,形状绘图,其他图标等)都应放在相应的可绘制文件夹中。