【问题标题】:Set source of ImageView in custom ListView after list bind列表绑定后在自定义 ListView 中设置 ImageView 的来源
【发布时间】:2014-05-20 15:56:57
【问题描述】:

我有一个与自定义适配器绑定的 ListView,它工作正常:

private class Placeslist extends ArrayAdapter<ArrayList<String>> {

    private ArrayList<ArrayList<String>> items;

    public Placeslist(Context context, int textViewResourceId,
            ArrayList<ArrayList<String>> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.locationgpsrow, null);
        }
        ArrayList<String> o = items.get(position);
        if (o != null) {
                        // Binding here
                        TextView tv1 = (TextView) v.findViewById(R.id.txt1);
                        tv1.setText(o.get(0));
                        ImageView imView = (ImageView) v.findViewById(R.id.imageView1);
                        //o.get(1) // Image url e.g. http://www.some-website/image.jpg
                     }
        return v;
    }
}

我的一个数组元素中有一个 图像源 url,我需要下载图像并将其设置为列表视图项自定义布局中的 ImageView。而且,我也有代码!!

ImageView imView = (ImageView) findViewById(R.id.imageView1);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);
            is.close();

imView.setImageBitmap(Bitmap.createScaledBitmap(bmImg, 宽度, 高度,真实));

我可以在上面的 getView() 中使用它来设置 imageView 源。但我想要的是让列表视图绑定到适配器而不加载图像(因为这将是繁重的过程)。然后在加载 listView 之后,分别启动绑定所有行中的 ImageView。他们是实现这一目标的方法还是任何可以将图像下载过程与 listView 绑定分开的方法?

【问题讨论】:

    标签: android listview android-listview imageview


    【解决方案1】:

    你必须在运行时使用下面的库来加载图像:-

    Universal loader lib

    Picasso

    Urlhelper

    【讨论】:

      【解决方案2】:

      您可以使用 3rd 方库,例如 universalimageloader 和其他库。

      否则,如果您想自己处理,只需创建一个 asyncTask 以从 url 下载图像并将其设置为 imageView onPostExecute 回调。可以将参数(ImageView img, String Url)传递给这个AsyncTask的Constructor,在DoinBackground()中下载图片,最后设置到img onPostExecute();

      您可以通过 Adapter 的 getView() 方法触发此 asyncTask,可能需要在 UI 线程上执行此操作。

      【讨论】:

        【解决方案3】:

        我使用了毕加索图像库,它可以轻松处理缓存和下载。看看这里: http://square.github.io/picasso/

        Universal Image Loader 也做得很好,配置选项更多: https://github.com/nostra13/Android-Universal-Image-Loader

        您可能想考虑使用这样的库,因为它可以保存数据,而不是执行太多的网络操作。

        【讨论】:

        • 谢谢!!我使用了通用加载器库
        【解决方案4】:

        使用 SmartImageView 将 url 加载到图像。 http://loopj.com/android-smart-image-view/

        【讨论】:

          【解决方案5】:

          您需要阅读有关异步加载图像的列表视图的延迟加载。您可以使用任何可用的库。

          关于列表视图和延迟加载的完整教程:

          http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多