如何在ListView中将图像加载到GridView中?

时间:2016-07-13 07:56:40

标签: android listview gridview

在ListView适配器中:

    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        final DataEntity data = dataList.get(position);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_list_asset_receivable, null);
            holder.amountTV = (TextView) convertView.findViewById(R.id.tv_asset_liability_fill_accounts_receivable_amount);
            holder.gridView = (CustomGridView) convertView.findViewById(R.id.cgv_images);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.amountTV.setText(String.valueOf(data.getAmount()));

        final String id = data.getPicGroupId();

        OkHttpUtilsHelper.getImagesByGroupId(data.getPicGroupId(), TAG)
                .execute(new ImageGroup.MyCallback() {
                    @Override
                    public void onResult(List<String> images) {
                        CustomGridViewAdapter adapter = new CustomGridViewAdapter(context, images);
                        holder.gridView.setAdapter(adapter);
                    }
                });

        return convertView;
    }
}

在GridView适配器中,我使用滑动将Internet图像加载到ImageView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.item_gridview_pics, null);
        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_pic);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    String imgPath = (String) dataList.get(position);
    if (imgPath.startsWith("http")) {
        Glide.with(context)
                    .load(imgPath)
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher)
                    .crossFade()
                    .into(viewHolder.imageView);
    } else {
        viewHolder.imageView.setImageBitmap(BitmapFactory.decodeFile(imgPath));
    }

    return convertView;
}

我已经解决了ListView与GridView问题的问题,如:Add a GridView to a ListView in Android

根据日志,似乎gridview适配器的getView()会向后触发listview适配器的getView(),然后再次调用gridview适配器的getview,这是一个契约循环!

欢迎任何建议!

4 个答案:

答案 0 :(得分:0)

您可以使用Picasso Library

示例代码:

Picasso.with(mContext) //
            .load(TextUtils.isEmpty(mList.get(position).getImage_url()) ? null : mList.get(position).getImage_url())
            //.load("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRp81DX-2x8DAJCmMCQKdYX3Hs1F-OGqhX_eYQnkbxtaX1dMrMNo8QMCY0")
            .tag(mContext) //
            .into(holder.imageViewDocumentIcon);

答案 1 :(得分:0)

您可以使用以下库,

  1. Picasso

  2. Android-Universal-Image-Loader

  3. Fresco

  4. Glide

  5. 这可能会对你有所帮助。

答案 2 :(得分:0)

更好地使用Aquery

aq.id(R.id.simpleLoadImg).image("http://programmerguru.com/android-tutorial/wp-content/uploads/2014/01/asynctask_thumb.png",false,false);

答案 3 :(得分:0)

这个问题并不意味着获得如何异步加载图像的解决方案,关键点是当listview内部视图 - gridview加载来自互联网的图像视图改变而listview将调用getView()方法再次,这将是一个死循环。

我发现你不能在getView()方法中做异步作业,这个方法不能加载回调结果,否则结果会再次触发getView()。

所以,我把这个异步作业移到了getView()之外:

  OkHttpUtilsHelper.getImagesByGroupId(data.getPicGroupId(), TAG)
            .execute(new ImageGroup.MyCallback() { 
                @Override 
                public void onResult(List<String> images) {
                    CustomGridViewAdapter adapter = new CustomGridViewAdapter(context, images);
                    holder.gridView.setAdapter(adapter);
                } 
            }); 

我刚收到结果并将其传递给listview适配器,问题解决了!