一:先导 imagerLoder 夹包
二:创建一个类 继承 Application
public class MyAppliction extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); } }
三:在适配器中要进行的操作:
Context context; List<MyBean.NewslistBean> list; public MyAdapter(Context context, List<MyBean.NewslistBean> list) { this.context = context; this.list = list; //重点options=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式 .displayer(new RoundedBitmapDisplayer(20))//设置圆角图片 .build();
}
@Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(context, R.layout.list_item, null); } ImageView img = (ImageView) convertView.findViewById(R.id.img); TextView tv = (TextView) convertView.findViewById(R.id.tv); //重点: ImageLoader.getInstance().displayImage(list.get(position).getPicUrl(),img); tv.setText(list.get(position).getTitle()); return convertView; }