首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinearLayout in ExpandableListView

LinearLayout in ExpandableListView
EN

Stack Overflow用户
提问于 2010-07-18 10:51:08
回答 1查看 1.4K关注 0票数 0

我想膨胀一个childView of ExpandableChildView组件。

代码:

代码语言:javascript
复制
    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对象的向量,我已经实例化了这些对象。

代码语言:javascript
复制
    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: ERROR/AndroidRuntime(2738):java.lang.IllegalStateException:指定的子程序已经有一个父级。您必须首先对孩子的父级调用removeView()。

我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-08 23:20:06

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

您没有提到,但我假设您的‘layout/widget_figre.xml’定义了如下结构:?

代码语言:javascript
复制
<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的部分,然后在getChildView()中制作一个switch case,然后动态地膨胀所需的布局。因为,即使在prepareThemes中存储非绑定布局时:一旦有多个组,将预存储的布局添加到snd组的子组时,也会得到相同的错误。

我希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3275204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档