1

我必须以编程方式将多个视图添加到HorizontalScrollView,但我无法两次添加相同的充气视图。所以我不得不重新膨胀我的XML布局N次。适配器解决方案不是一种选择。在Android中回收充气布局

有没有方法可以回收这个布局而不需要重新充气?

感谢所有。

UPDATE:我的代码是这样的。

View view = getLayoutInflater().inflate(R.layout.my_view, null); 
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll); 

for(int i = 0; i < 25; i++) 
{ 
    // Process textview and images inside the view 
    h.addView(view); 
} 
+0

是否有这个应用您的任何代码?你如何夸大你的布局? – rogcg

+0

基本上就是这样 –

回答

0

我不知道这是你正在寻找我们可以膨胀的布局像这样

LayoutInflater.from(context).inflate(R.layout.abc_action_bar_decor, parent, true); 
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll); 
LinearLayout parent= new LinearLayout(context); 
parent.setOrientation(LinearLayout.HORIZONTAL); 
h.addView(parent); 
for(int i = 0; i < 25; i++) 
{ 
    View view = getLayoutInflater().inflate(R.layout.my_view, parent, true); 
    // Process textview and images inside the view 
} 
+0

这就是我想要避免的:使用膨胀函数n次,我想膨胀一次我的布局,并在for循环中使用它 –

+0

@OllieStrevel这不会工作,对于每个可显示的列表项需要不同的视图对象,因此您需要为每个项目填充布局。 – DrNachtschatten