2012-09-07 58 views
2

我想每个组像显示不同的子布局:Android的可扩展列表:组不同的布局

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

      if (convertView == null) { 

       switch (groupPosition){ 

       case 0: 
        LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater.inflate(R.layout.child_row, null); 
        TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName); 
        tvPlayerName.setText(arrChildelements[groupPosition][childPosition]); 
        break; 

       case 1: 
        LayoutInflater inflater1 = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater1.inflate(R.layout.child_row1, null); 
        TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName); 
        tvPlayerName1.setText(arrChildelements[groupPosition][childPosition]); 
        break; 

       case 2: 
        LayoutInflater inflater2 = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater2.inflate(R.layout.child_row2, null); 
        TextView tvPlayerName2 = (TextView) convertView.findViewById(R.id.tvPlayerName); 
        tvPlayerName2.setText(arrChildelements[groupPosition][childPosition]); 
        break; 

       case 3:    
        LayoutInflater inflater3 = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater3.inflate(R.layout.child_row3, null); 
        TextView tvPlayerName3 = (TextView) convertView.findViewById(R.id.tvPlayerName); 
        tvPlayerName3.setText(arrChildelements[groupPosition][childPosition]); 
        break; 

       } 


      } return convertView; 
    } 

我的问题是,而在群体点击,孩子名单群体之间的相互变化。

有人可以告诉我,我做错了什么?

回答

7

正是因为

if (convertView == null) 

删除它,它应该正常工作,如果它的工作,因为它应该if语句删除此之后,返回并添加if语句里面,如果(convertView == NULL)至使它看起来像你想要的一样,因为如果(convertView == null)会使滚动速度慢得多并使用更多的内存。

例如,您可以将所有视图放在一个xml文件或一个视图中,然后在switch方法中使用setVisibility来显示适合当前列表项的布局部分。

+0

这是工作,如果去除(convertView == NULL)后,按预期工作正常。 我不明白,如果(convertView == null),我应该添加什么内容,使其使用较少的内存。 – Kishore

+0

我必须展示7种不同的布局,这将很难把所有的观点放在一个XML中。 – Kishore

3

如果(convertView == NULL)

从定制适配器在子视图删除

它所有