I am currently work on a horizontal listview(a bookshelf like app), but it does not matter, just use the vertical listview as example. Imagine I have an UI like this:
listivew 1 | listview 2| listview 3
on each listview there are 10 images , so the UI is like this
img 1 | img 11 | img 21
img 2 | img 12 | img 22
img 3 | img 13 | img 23
.... | .... | ....
There are three listview that divide the screen , for each of them it show a list of image, the problem is , when I start loading it only load the image that within my screen,
e.g. if my screen can show 5 image then it loads the img 1 to img 5,
and then it will load the img 11 to img 15,
so it has no image when I scroll the listview 1 to bottom ,
the img 5 to 10 is load only when I finish load 11 to 15.
The problem is seems caused by the ordering of the getView(), are there any opition to interrupt the getView() so that once I scroll , I add the priority to the listview 1 , instead of still loading the image in listview 2? Thanks
Adapter
private class PublishmentAdapter extends ArrayAdapter<Publishment> {
List<Publishment> mItems;
LayoutInflater mInflater;
int mResource;
String pubType;
public PublishmentAdapter(Context context, int resourceId,List<Publishment> objects, String toGetType) {
super(context, resourceId, objects);
mInflater = LayoutInflater.from(context);
mResource = resourceId;
mItems = objects;
pubType = toGetType;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//if (convertView == null) {
convertView = mInflater.inflate(mResource, parent, false);
//}
ImageView publishImg = (ImageView) convertView.findViewById(R.id.publishImg);
Bitmap tempImg = null;
if (pubType.equals("book"))
tempImg = gs.getBookImgList(position);
else if (pubType.equals("poster"))
tempImg = gs.getPosterList(position);
else if (pubType.equals("other"))
tempImg = gs.getOtherList(position);
if (tempImg == null)
new ImageLoader(ctxActivity,pubType).execute(publishImg, getItem(position).imgURL);
else
publishImg.setImageBitmap(tempImg);
convertView.setTag(getItem(position).id);
return convertView;
}
}
Image loader
public class ImageLoader extends AsyncTask<Object, Void, Bitmap> {
private static String TAG = "ImageLoader";
private InputStream input;
private ImageView view;
private String imageURL;
private String type;
private MyApp gs;
private Context ctx;
public ImageLoader(Activity _ctx,String _type){
ctx = _ctx;
gs = (MyApp) _ctx.getApplication();
type = _type;
}
@Override
protected Bitmap doInBackground(Object... params) {
try {
view = (ImageView) params[0];
//handle Chinese characters in file name
String[] imgUrlArray = ((String) params[1]).split("/");
String fileName = imgUrlArray[imgUrlArray.length - 1];
String newfileName = URLEncoder.encode(fileName,"utf-8");
imageURL = ((String) params[1]).replace(fileName, newfileName);
URL url = new URL(imageURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null && view != null) {
if (type.equals("video"))
gs.setVideoImg(result);
else if (type.equals("latestPub"))
gs.setlatestPubImg(result);
else if (type.equals("book"))
gs.addBookImgList(result);
else if (type.equals("poster"))
gs.addPosterList(result);
else if (type.equals("other"))
gs.addOtherList(result);
} else if (type.equals("book")){
gs.addBookImgList(BitmapFactory.decodeResource(ctx.getResources(),R.drawable.ic_launcher));
} else if (type.equals("poster")){
gs.addPosterList(BitmapFactory.decodeResource(ctx.getResources(),R.drawable.ic_launcher));
} else if (type.equals("other")){
gs.addOtherList(BitmapFactory.decodeResource(ctx.getResources(),R.drawable.ic_launcher));
}
view.setImageBitmap(result);
}
}