【问题标题】:Recycle an inflated layout in Android在 Android 中回收膨胀的布局
【发布时间】:2014-05-02 18:05:24
【问题描述】:

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

有没有办法在不重新膨胀的情况下回收这个布局?

谢谢大家。

更新:我的代码是这样的。

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

【问题讨论】:

  • 你的这个应用程序中有代码吗?你是如何夸大你的布局的?
  • 基本上是这样的

标签: android android-layout android-intent android-fragments android-inflate


【解决方案1】:

我不确定你在寻找什么,我们可以像这样膨胀布局

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
}

【讨论】:

  • 这是我想要避免的:使用 inflate 函数 n 次,我想将我的布局膨胀一次并在 for 循环中重新使用它
  • @OllieStrevel 这行不通,对于每个可显示的列表项,您需要一个不同的 View 对象,因此您需要为每个项目扩展布局。
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多