2010-07-18 55 views
0

我想膨胀ExpandableChildView组件的childView。ExpandableListView中的LinearLayout

代码:

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: ERROR/AndroidRuntime(2738): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我怎么能解决这个问题?

回答

0

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

你没有提到,但我认为你的'layout/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组的子项添加预先存储的布局时,您会得到相同的错误。

我希望这会有所帮助。