我目前正在处理横向列表视图(类似app的书架),但没关系,只需使用垂直列表视图作为示例。想象一下,我有一个像这样的用户界面:
listivew 1 | listview 2| listview 3
在每个列表视图上有10个图像,因此UI就像这样
img 1 | img 11 | img 21
img 2 | img 12 | img 22
img 3 | img 13 | img 23
.... | .... | ....
有三个listview划分屏幕,每个都显示一个图像列表,问题是,当我开始加载它时只加载我屏幕内的图像,
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.
问题似乎是由getView()的排序引起的,是否有任何中断getView()的选择,这样一旦我滚动,我将优先级添加到listview 1,而不是仍然在listview中加载图像2?感谢
适配器
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;
}
}
图片加载器
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);
}
}