【问题标题】:Create duplicates of already attached view创建已附加视图的副本
【发布时间】:2014-09-16 20:42:52
【问题描述】:

我目前正在尝试扩充一个布局,我最终将添加到一个垂直的 LinearLayout 容器中,作为更大的动态表单创建模块的一部分。

layout_checkbox.xml 如下:

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="sample text" />

<LinearLayout
    android:id="@+id/checkboxGroup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="sample" />
</LinearLayout>

如果可能的话,现在我想做的是获取复选框 (@+id/checkbox) 并创建它的副本,所有这些都在 LinearLayout @+id/checkboxGroup 下。

layout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
LinearLayout checkboxGroup = (LinearLayout) layout.findViewById(R.id.checkboxGroup);
CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);
// code that supposedly clones/duplicates checkbox.

我知道我可以轻松地从另一个 xml 定义中扩充 Checkbox 以创建它的多个实例,然后将它们添加到组中。我尝试这样做的原因是因为我正在创建某种类型的库,并且我正在尝试强制执行某种约定以使其更易于使用。

编辑:

这行得通,但它很容易闻到性能下降的味道:

layout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
LinearLayout originalCheckboxGroup = (LinearLayout) layout.findViewById(R.id.checkboxGroup);
for (String entryItem : entry.fieldEntries) {
    LinearLayout tempCheckboxLayout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
    LinearLayout checkboxGroup = (LinearLayout) tempCheckboxLayout.findViewById(R.id.checkboxGroup);
    CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);
    checkbox.setText(entryItem);
    checkboxGroup.removeView(checkbox);
    originalCheckboxGroup.addView(checkbox);
}

【问题讨论】:

标签: android android-view


【解决方案1】:
what I want to do if possible is to get hold of the Checkbox (@+id/checkbox) and create copies of it

不,您不能复制/克隆视图,因为您将始终并且将对视图具有相同的引用,并且当一个更改全部更改时,唯一的方法是每次都为视图充气使用相同的视图。

编辑:

没有办法克隆/复制复选框,但您可以在每次要使用复选框时扩充视图(线性布局)。并使用被夸大的复选框。每次你要使用/克隆一个复选框时都这样做。

【讨论】:

  • 这就是我想要一个 CLONE 的原因,因为我不想要另一个 Checkbox 引用我在 xml 中定义的同一对象。我正在寻找可以给我一个 Checkbox 的新实例的东西,在我的 xml 中具有 Checkbox 的所有相同格式等,但不附加到容器。
  • "但未附加到容器"??每次你想要一个新的实例时,只需给它充气。
  • 正如我的问题中提到的,我知道我可以膨胀另一个 xml 并将其附加到布局中。这将需要我定义另一个可能的 checkbox_item.xml,我试图避免它以最小化复杂性(不是为了我自己,而是为了我试图创建的库的用户)。
  • @josephus 见上面的编辑。顺便说一句,你不能做你想做的事,唯一的解决办法是每次你想要一个克隆到 cehckbox 时膨胀 LinearLayout
  • 我最初可能误解了你的答案,但我现在明白了。诀窍是为我要添加的每个复选框膨胀相同的 LinearLayout,获取其中的 Checkbox,将其从父级中删除,然后将其添加到我的第一个 LinearLayout。如果您编辑答案,我会接受。 :) 谢谢!
【解决方案2】:

可能会为复选框的文本添加一个列表

  private LinearLayout checkboxGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);

    checkboxGroup = (LinearLayout) findViewById(R.id.checkboxGroup);
    CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);

    List<String> list = null;  //set it 
    addCheckBox(checkbox, list);

}

private void addCheckBox(CheckBox checkbox, List<String> checkBoxTextList) {

    for (int index = 0; index < checkBoxTextList.size(); index++) {
        CheckBox checkboxClone = checkbox;
        checkboxClone.setText(checkBoxTextList.get(index));
        checkboxClone.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

            }
        });

        checkboxGroup.addView(checkboxClone);
    }

}

【讨论】:

  • 'CheckBox checkboxClone = checkbox;'的问题它仍然是被引用的同一个对象吗?另外,调用 addView 来附加一个已经有父视图的视图会导致错误。
猜你喜欢
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 2021-05-22
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多