///baseadapter//
package com.example.day20_rk; import android.content.Context; import android.graphics.Bitmap; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.day20_rk.base.BaseActivity; import com.example.day20_rk.user.MyTask; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /** * Created by Administrator on 2017/10/24 0024. */ public class MyAdapter extends BaseAdapter { private DisplayImageOptions displayImageOptions; private Context context; private List<Bean.ResultBean.DataBean> list; public MyAdapter(Context context, List<Bean.ResultBean.DataBean> list) { this.context = context; this.list = list; displayImageOptions=new DisplayImageOptions.Builder() .cacheInMemory(true)//进行内存缓存 .cacheOnDisk(true)//进行sd卡缓存 // .showImageOnLoading(R.drawable.ic_stub) // 设置正在下载中的图片 // .showImageForEmptyUri(R.drawable.ic_empty) //没有请求地址时 //.showImageOnFail(R.drawable.ic_error)//下载错误时 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片质量 .build(); } private int Image_one=0; private int Image_two=1; @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { if(position%4==0){ return Image_one; }else if(position%4==1){ return Image_one; }else if(position%4==2){ return Image_two; } return Image_two; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { int i=getItemViewType(position); if(i==Image_one){ ViewHolder holder; if(convertView==null){ convertView=View.inflate(context,R.layout.item1,null); holder=new ViewHolder(); holder.img= (ImageView) convertView.findViewById(R.id.img); holder.wenzi= (TextView) convertView.findViewById(R.id.wenzi); convertView.setTag(holder); }else{ holder= (ViewHolder) convertView.getTag(); } holder.wenzi.setText(list.get(position).getTitle()); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.img,displayImageOptions); }else if(i==Image_two){ ViewHolder2 holder; if(convertView==null){ convertView=View.inflate(context,R.layout.item2,null); holder=new ViewHolder2(); holder.img2= (ImageView) convertView.findViewById(R.id.img2); holder.wenzi2= (TextView) convertView.findViewById(R.id.wenzi2); convertView.setTag(holder); }else{ holder= (ViewHolder2) convertView.getTag(); } holder.wenzi2.setText(list.get(position).getTitle()); ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.img2,displayImageOptions); } return convertView; } class ViewHolder{ ImageView img; TextView wenzi; } class ViewHolder2{ ImageView img2; TextView wenzi2; } }///入口函数//
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); File cacheDir = new File(Environment.getExternalStorageDirectory(),"image"); if (! cacheDir.exists()){ cacheDir.mkdirs(); } ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .memoryCache(new LruMemoryCache(2 * 1024 * 1024))//设置内存缓存区大小 .memoryCacheSize(2 * 1024 * 1024)//设置缓存区大小 .memoryCacheExtraOptions(320,320)//缓存图片最大的宽度与高度 px .diskCacheSize(50*1024*1024)//设置sd卡缓存的空间大小 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//sd卡缓存图片的命名 使用md5加密方式 .diskCache(new UnlimitedDiscCache(cacheDir))//自定义sd卡的缓存路径 .diskCacheFileCount(100)//缓存文件的最大数量 .writeDebugLogs()//写入日志 .threadPoolSize(3)//线程池 .build(); //对imageLoader进行初使化 ImageLoader.getInstance().init(configuration); } }