如何膨胀充气布局中具有相同ID的布局多个实例 [英] How to inflate multiple instances of a layout with the same id inside an inflated layout

查看:151
本文介绍了如何膨胀充气布局中具有相同ID的布局多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多嵌套LinearLayouts和的LinearLayout TextViewss

I have a LinearLayout with many nested LinearLayouts and TextViewss

我的主要活动膨胀主要的LinearLayout,

My main activity inflates the main LinearLayout,

然后,我从服务器中载入数据,并根据收到的数据,我在一个占位符(LinearLayout中)添加多个布局

Then I load data from a server and based on the data received, I add multiple Layouts in a place holder (LinearLayout)

这是简单的一个新闻页面,在这里我加载与新闻相关的图像,并把它里面的一个初始为空的LinearLayout。

This is simple a news page where I load Images associated with the news and place it inside an initially empty LinearLayout.

每个图片有如下信息:标题(TextView的),日期(TextView的),图像(ImageView的),所以我实际上做的是以下内容:

Each Image has the following info: Title(TextView), Date(TextView), Image(ImageView) so what I actually do is the following:

* 请注意,这只是必要的codeD上的问题,我elemenated所有的尝试 - >抓...的if / else ....等

public void addImages(JSONArray images){
      ViewGroup vg = (ViewGroup) findViewById(R.id.imagesPlaceHolder);


      // loop on images
      for(int i =0;i<images.length;i++){

          View v = getLayoutInflater().inflate(R.layout.image_preview,vg);
          // then 
          I think that here is the problem 
          ImageView imv = (ImageView) v.findViewById(R.id.imagePreview);
          TextView dt = (TextView) v.findViewById(R.id.dateHolder);
          TextView ttl = (TextView) v.findViewById(R.id.title);
          // then 
          dt.setText("blablabla");
          ttl.setText("another blablabla");
          // I think the problem is here too, since it's referring to a single image
          imv.setTag( images.getJSONObject(i).getString("image_path").toString() );
          // then Image Loader From Server or Cache to the Image View

      }
}

在code以上的作品好,单个图像

The code above works good for a single image

但对于多个图像的图像装载机不工作,我想这是因为所有的ImageViews(充气多次)有相同的ID

But for multiple images the Image Loader doesn't work I guess it's because all ImageViews (Inflated multiple times) have the same ID

推荐答案

还有一个原因,为什么在布局XML中的ImageView需要有一个ID?你能清除机器人:从image_ preview.xml布局ID属性,然后只需通过膨胀的LinearLayout的孩子重复?例如:

Is there a reason why the ImageView in the layout XML needs to have an ID? Could you erase the android:id attributes from the image_preview.xml layout and then simply iterate through the children of the inflated LinearLayout? For example:

ViewGroup v = (ViewGroup)getLayoutInflater().inflate(R.layout.image_preview,vg);
ImageView imv = (ImageView) v.getChildAt(0);    
TextView dt = (TextView) v.getChildAt(1);
TextView ttl = (TextView) v.getChildAt(2);

这篇关于如何膨胀充气布局中具有相同ID的布局多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆