1

我目前正在研究水平列表视图(类似书架的应用程序),但这没关系,只需以垂直列表视图为例。想象一下,我有一个这样的 UI:

listivew 1 | listview 2| listview 3

每个 listview 有 10 张图片,所以 UI 是这样的

img 1 | img 11 | img 21 
img 2 | img 12 | img 22
img 3 | img 13 | img 23
....  | ....   | ....

有三个划分屏幕的列表视图,每个列表视图都显示一个图像列表,问题是,当我开始加载它时,它只加载我屏幕内的图像,

 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);
}

}

4

0 回答 0