2012-09-07 54 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)

從定製適配器在子視圖刪除

它所有