GridView 从 URL 加载图像。当我滚动gridview时,更多的图像机会,如何修复它......我已经尝试了imageAdapter.notifyDataSetChanged(),gridView.invalidateViews()..我还没有找到解决这个问题的方法。
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<NodeFood> listImage = new ArrayList<NodeFood>();
public ImageAdapter(Context c, ArrayList<NodeFood> listImage){
this.mContext = c;
this.listImage = listImage;
}
public class ViewHolder{
ImageView imageView;
ProgressBar progress;
}
public int getCount() {
return listImage.size();
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
if(arg1 == null){
holder = new ViewHolder();
mInflater = LayoutInflater.from(mContext);
arg1 = mInflater.inflate(R.layout.layout_item_grid, null);
holder.imageView = (ImageView)arg1.findViewById(R.id.imageItemGrid);
holder.progress = (ProgressBar)arg1.findViewById(R.id.progressBar);
holder.progress.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);
arg1.setTag(holder);
}else{
holder = (ViewHolder)arg1.getTag();
}
holder.imageView.setTag(arg0);
holder.progress.setTag(arg0);
NodeFood a = this.listImage.get(arg0);
String URL = a.getSRC();
task_LoadIMG bb = new task_LoadIMG(URL,holder);
bb.execute();
return arg1;
}
public class task_LoadIMG extends AsyncTask<Void,Void,Bitmap>{
private String url;
private ViewHolder holder;
public task_LoadIMG(String url, ViewHolder holder){
this.url = url;
this.holder = holder;
}
@Override
protected Bitmap doInBackground(Void... arg0) {
try{
URL aURL = new URL(this.url);
URLConnection connect = aURL.openConnection();
connect.connect();
InputStream is = connect.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
return bm;
}catch(IOException e){
return null;
}
}
@Override
protected void onPostExecute(Bitmap result) {
holder.progress.setVisibility(View.GONE);
holder.imageView.setImageBitmap(result);
holder.imageView.setScaleType(ScaleType.CENTER_CROP);
}
}
}