【问题标题】:LinearLayout in ExpandableListViewExpandableListView 中的线性布局
【发布时间】:2010-07-18 10:51:08
【问题描述】:

我想为 ExpandableChildView 组件的 childView 充气。

代码:

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        View v = convertView;
        LinearLayout linearOpt = themesOptions.get(childPosition);

        if(v == null) {
            v = mInflater.inflate(R.layout.itemrow, null);
        }

        LinearLayout ll = (LinearLayout) v.findViewById(R.id.root);
        ll.addView(linearOpt);
        return v;

    }

其中 linearOpt 是一个向量,其中包含许多我已实例化的 LinearLayout 对象。

    private void prepareThemes(){
        View v = mInflater.inflate(R.layout.widget_configure, null);
        LinearLayout theme = (LinearLayout) v.findViewById(R.id.themeLayout);
        LinearLayout color = (LinearLayout) v.findViewById(R.id.colorLayout);
        LinearLayout trans = (LinearLayout) v.findViewById(R.id.transpLayout);

        themesOptions.add(theme);
        themesOptions.add(color);
        themesOptions.add(trans);

    }

这是 R.layout.itemrow xml:

但是我收到了这个错误:

07-18 10:48:49.740: 错误/AndroidRuntime(2738): java.lang.IllegalStateException: 指定的孩子已经有一个父母。 您必须在 首先是孩子的父母。

我该如何解决这个问题?

【问题讨论】:

    标签: android android-linearlayout expandablelistview inflate


    【解决方案1】:

    我认为问题是由您在 prepareThemes() 中准备布局的方式引起的。

    您没有提到,但我假设您的“布局/widget_configure.xml”定义了这样的结构:?

    <LinearLayout android:id="@+id/root">
        <LinearLayout android:id="@+id/themeLayout> <!-- ... --> </LinearLayout>
        <LinearLayout android:id="@+id/colorLayout> <!-- ... --> </LinearLayout>
        <LinearLayout android:id="@+id/transpLayout> <!-- ... --> </LinearLayout>
    </LinearLayout>
    

    然后你在prepareThemes() 中扩充它以获得 3 个子布局。但是此时它们已经注册为周围布局的子级(我称之为“根”)。正如错误所暗示的那样,视图实例只能添加到一个父级

    您可以将每个 LinearLayout 存储在其自己的 xml 文件中,然后膨胀 3 次。然后,这些存储的布局可以只添加一次给一个孩子。

    我不确定你想做什么,但我想,最好 - 而不是准备 - 只拥有 3 个不同的布局,包括来自 layout.itemrow 的部分,然后制作一个 switch casegetChildView() 中,并即时填充所需的布局。因为即使您在 prepareThemes 中存储了一个未绑定的布局:只要您有一个以上的组,在将预存储的布局添加到 snd 组的子组时,您也会遇到相同的错误。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多