在Android

时间:2016-01-26 22:23:32

标签: android image listview reload

我有一个listView,我将标题和描述与图片放在一起,当我向上滚动时图片重新加载,我该如何解决这个问题?我尝试了一些解决方案,但他们失败了......

这是我的代码:

GETVIEW

public class ViewHolder extends ArrayAdapter<ListViewModel> {

    public ViewHolder(Context context, List<ListViewModel> model) {
        super(context, 0, model);
    }

    @Override
    public View getView(int position, View cView, ViewGroup parent) {

        if(cView == null){
            cView = LayoutInflater.from(getContext()).inflate(R.layout.view_list_holder,parent, false);
        }
        ListViewHolder viewHold = (ListViewHolder) cView.getTag();
        if(viewHold == null){
            viewHold = new ListViewHolder();
            viewHold.title = (TextView) cView.findViewById(R.id.title);
            viewHold.content = (TextView) cView.findViewById(R.id.content);
            viewHold.picture = (ImageView) cView.findViewById(R.id.picture);
            cView.setTag(viewHold);
        }

        ListViewModel model = getItem(position);
        viewHold.title.setText(model.getTitle());
        viewHold.content.setText(model.getContent());
        if (viewHold.picture != null)
            new DownloadImageTask(viewHold.picture).execute(model.getPicture());
        return cView;
    }

    private class ListViewHolder{
        public TextView title;
        public TextView content;
        public ImageView picture;
    }
}

ImageDownloader

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap myImage = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            myImage = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myImage;
    }

    protected void onPostExecute(Bitmap result) {

        bmImage.setImageBitmap(result);
    }
}

2 个答案:

答案 0 :(得分:0)

你能不能将图像保存在活动中的位图数组中?然后只显示那些。因此,他们从不重新加载,他们只是在视图中使用的活动?

答案 1 :(得分:0)

尝试使用Volley而不是AsyncTask:

http://developer.android.com/training/volley/request.html

如果用户滚动了,如果不再需要特定的一组图像,它已内置支持图像缓存以及取消请求。