【问题标题】:Android : Listadapter loading downloaded imagesAndroid : Listadapter 加载下载的图像
【发布时间】:2013-12-04 11:29:52
【问题描述】:

我尝试使用 BaseAdapter 创建自定义 Listadapter ,在这个适配器中 imagesURLs 和适配器必须下载所有东西然后更新视图..

一切都已完成并且工作正常,但我的问题是 我无法移动列表视图当图像仍在加载我应该等待完成加载所有内容然后我可以移动..

我认为问题的发生是因为列表视图在每次更新时都返回顶部索引,因为我更新了每个下载完成的图像,但我不确定因为列表冻结所以我无法移动它来确认,或者有别的东西..

我使用更新列表适配器:

adap.notifyDataSetChanged();

这是所有细节,我希望任何人都知道我该如何解决这个问题,帮助我并告诉我什么是更新视图没有问题的正确方法(返回第一个索引,冻结)..

注意:下载没有问题,我可以移动列表时图片下载只有在加载和更新时出现问题。

解决:

我检查了我的代码,下载功能正常,更新适配器冻结:

adp.notifyDataSetChanged(); 

更新需要几秒钟,所以我改变了更新方式:

int index = lst.getFirstVisiblePosition();
    View v = lst.getChildAt(0);
    int top = (v == null) ? 0 : v.getTop();
    // ...
   adap.notifyDataSetInvalidated();
    // restore
    lst.setSelectionFromTop(index, top);

【问题讨论】:

    标签: android listview listadapter baseadapter custom-adapter


    【解决方案1】:

    我使用this 库来异步下载图片。

    您可以检查我的代码并将其修改为适合您的项目。

    class WallAdapter extends BaseAdapter {
    
    WallInfo wallInfo;
    Activity activity;
    LayoutInflater inflater=null;
    
    public WallAdapter(Activity a, WallInfo wallInfo) {
        this.activity = a;
        this.wallInfo = wallInfo;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount() {
        return wallInfo.getCount();
    }
    
    @Override
    public Object getItem(int position) {
        return wallInfo.getItem(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView == null)
            vi = inflater.inflate(R.layout.wall_item, null);
    
        ImageView imagePost = (ImageView) vi.findViewById(R.id.imagePost);
    
        WallMessage post = wallInfo.getItem(position);
    
        if(post.attachments != null && post.attachments.size() != 0) {
            UrlImageViewHelper.setUrlDrawable(imagePost, post.attachments.get(0).photo.src_big);
        } else {
            imagePost.setImageBitmap(null);
        }
        return vi;
    }
    

    }

    【讨论】:

      【解决方案2】:

      尝试使用 google volley 库,它将帮助您更快地下载。

      【讨论】:

        猜你喜欢
        • 2012-12-01
        • 2015-12-13
        • 1970-01-01
        • 2017-04-07
        • 1970-01-01
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多