Android 如何在膨胀的布局中膨胀具有相同id的布局的多个实例

Android 如何在膨胀的布局中膨胀具有相同id的布局的多个实例,android,android-layout,android-view,android-inflate,Android,Android Layout,Android View,Android Inflate,我有一个带有许多嵌套的LinearLayout和TextViews的LinearLayout 我的主要活动是膨胀主线布局 然后,我从服务器加载数据,并根据接收到的数据,在占位符中添加多个布局(LinearLayout) 这是一个简单的新闻页面,我在其中加载与新闻相关联的图像,并将其放置在最初为空的LinearLayout中 每个图像都有以下信息:标题(TextView)、日期(TextView)、图像(ImageView),因此我实际做的是: *请注意,这只是我将所有try->catch元素化的

我有一个带有许多嵌套的LinearLayout和TextViews的LinearLayout

我的主要活动是膨胀主线布局

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

这是一个简单的新闻页面,我在其中加载与新闻相关联的图像,并将其放置在最初为空的LinearLayout中

每个图像都有以下信息:标题(TextView)、日期(TextView)、图像(ImageView),因此我实际做的是:

*请注意,这只是我将所有try->catch元素化的问题中的基本代码。。。如果/否则…等

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

      }
}
public void添加图像(JSONArray图像){
ViewGroup vg=(ViewGroup)findViewById(R.id.imagesPlaceHolder);
//循环图像

对于(int i=0;i是否存在布局XML中的ImageView需要ID的原因?是否可以从image_preview.XML布局中删除android:ID属性,然后简单地迭代膨胀的LinearLayout的子级?例如:

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);

提供要用作父视图的视图组时,
inflate()
返回的视图就是此父视图(
vg
)而不是新创建的视图。因此,
v
指向视图组
vg
,而不是指向新创建的视图,并且由于您的所有子视图都具有相同的
id
,因此每次都返回相同的子视图(
imv
dt
ttl

两种解决方案。第一种是在完成
id
之后,在下一次迭代之前更改它们。因此,在下一次迭代开始时的下一次创建中,新创建的视图将具有与旧视图不同的id,因为它们仍将使用
R
中定义的旧常量

另一种解决方案是将参数false添加到对inflate()的调用中,以便新创建的视图不会附加到视图组,然后由inflate()返回函数,而不是视图组。然后,您的其余代码将以有人参与的方式工作,但您必须在迭代结束时将它们附加到视图组


请注意,您仍然需要提供一个视图组,因为它将用于确定LayoutParams的值。

我也有同样的问题,根据@SylvainL的回答,这里有一个可行的解决方案:

// myContext is, e.g. the Activity.
// my_item_layout has a TextView with id='text'
// content is the parent view (e.g. your LinearLayoutView)
// false means don't add direct to the root
View inflated = LayoutInflater.from(myContext).inflate(R.layout.my_item_layout, content, false);

// Now, before we attach the view, find the TextView inside the layout.
TextView tv = (TextView) inflated.findViewById(R.id.text);
tv.setText(str);

// now add to the LinearLayoutView.
content.addView(inflated);

我使用dynnamic扩展XML布局并获得id的文本

  private val onAddView = View.OnClickListener {
    val parent = viewForm.findViewById<LinearLayout>(R.id.layout_parent)
    LayoutInflater.from(activity).inflate(R.layout.layout_child, parent) // layout_child has id "tv_attribute"
  }

  private val onSave = View.OnClickListener {
    val parent = viewForm.findViewById<LinearLayout>(R.id.layout_parent)
    for (i in 0 until parent.childCount) {
        val getText = parent.getChildAt(i).findViewById<TextView>(R.id.tv_attribute).text
    }
  }
private val onAddView=View.OnClickListener{
val parent=viewForm.findViewById(R.id.layout\u parent)
LayoutFlater.from(活动).充气(R.layout.layout\u子项,父项)//layout\u子项具有id“tv\u属性”
}
private val onSave=View.OnClickListener{
val parent=viewForm.findViewById(R.id.layout\u parent)
for(在parent.childCount之前0中的i){
val getText=parent.getChildAt(i).findViewById(R.id.tv_属性).text
}
}

您为什么不使用listview?是的,我在Scrollview中有一个线性布局,上面有一些文本视图,然后我有很多图像视图,甚至视频支架,listview在Scrollview中也有自己的滚动条,这对可用性不利(如果我是对的话)是的,我的视图嵌套在我上面的解决方案中,但我担心的是:我如何知道在我使用的1001到1000+x或2001到2000+x的范围内是否有自动生成的整数。我注意到自动生成的ID通常有非常大的值。请使用findViewById()检查它:如果没有具有所需整数值的ID,它将返回null。但是,在您的情况下,重要的是该值(1000+i)与R.ID.imagePreview不同。在迭代结束时更改“ID”完成了此操作….:p仍然像一个补丁:p@Shehabix如果要使用未使用的ID,请查看.generateView()生成一个适合在setId(int)中使用的值。该值不会与aapt在构建时为R.ID生成的ID值冲突。“如果膨胀相对布局,两种建议的解决方案似乎都不起作用。当替换ID时,相对属性(例如toRightOf)似乎不起作用。如果在充气时指定true添加到rootView,布局也会混乱。