2014-01-28 38 views
2

問題很簡單,但我無法使其工作。 我有一個3頭擴展列表,他們可以填充或空。可擴展列表中空組的替代視圖

我希望展示一個簡單的textview,當你展開一個空的組時,我一直在尋找一段時間,但似乎沒有任何工作。 我不能使用setEmpty(),因爲這隻有在所有的組都是空的時纔有效。

我發現了一個類似的主題在這裏:ExpandableListView - empty group message

如果我嘗試做的是接受的答案建議,所以:

@Override 
public int getChildrenCount(int groupPosition) { 
    if(this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .size() != 0){ 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size();   
    } 
    return 1; 
} 

我得到一個錯誤,當我嘗試展開空組..而且我不認爲這會起作用,例如,如果我有一個孩子的團體會發生什麼事情,會考慮爲空嗎?

編輯:

好,我解決了這個由始終在內角子列表的第一位有一個空的對象,那麼當getchildrencount返回1,我知道它實際上是空的,所以我把開關的空情況。 在開關的填充情況下,我總是隱藏第一個元素,但這可能不是最簡單的方法,但這是有效的。

回答

1

如果您正在使用不同的佈局是否被填充或空的,你可以通過重寫getChildTypegetGroupType這樣

private static final int CHILD_TYPE_COUNT = 2; 

@Override 
public int getChildType(int groupPosition, int childPosition) { 
    if(obj.isEmpty()) 
     return 0; 
    else if(obj.isPopulated) 
     return 1; 
} 

@Override 
public int getChildTypeCount() { 
    return CHILD_TYPE_COUNT; 
} 

然後你就可以使用相應的佈局來填充區分。我建議你也使用ViewHolder-Pattern

static class PopulatedViewHolder { 
    //declare the elements of a populated view here 
} 

static class EmptyViewHolder { 
    //declare the elements of an empty view here 
} 


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

    EmptyViewHolder emptyViewHolder = null; 
    PopulatedViewHolder populatedViewHolder = null; 

    //check if the type of your view is populated or empty and use the corresponding layout 
    int type = getChildType(groupPosition, childPosition); 

    if (convertView == null || (type == 0 && (convertView.getTag()) instanceof PopulatedViewHolder) || (type == 1 && (convertView.getTag()) instanceof EmptyViewHolder)) { 
     LayoutInflater inflater = (LayoutInflater)context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     switch(type){ 
      case 0: 
       //type is empty 
       emptyViewHolder = new EmptyViewHolder(); 
       convertView = inflater.inflate(R.layout.empty_layout, 
         parent, false); 

       //...initialize your viewcomponents of the populated layout here... 

       convertView.setTag(emptyViewHolder); 
       break; 
      case 1: 
       //populated 
       populatedViewHolder = new PopulatedViewHolder(); 
       convertView = inflater.inflate(
         R.layout.populated_layout, parent, false); 

       //...initialize your viewcomponents of the populated layout here... 

       convertView.setTag(populatedViewHolder); 

       break; 
     } 
    } 
    else { 
     switch (type) { 
      case 0: 
       emptyViewHolder = (EmptyViewHolder)convertView.getTag(); 
       break; 

      case 1: 
       PopulatedViewHolder = (PopulatedViewHolder)convertView.getTag(); 
       break; 
     } 
    } 

    switch(type){ 
     case 0: 
      //empty 
      //set the values of your empty view here 

      break; 
     case 1: 
      //populated 
      //set the values of your populated view here 

      break; 
    } 

    return convertView; 
}  
+0

我理解你的代碼,我也認爲這應該可行,但由於某些原因,這不是... 該應用程序的工作原理,當填充的組展開它工作正常,但是當我嘗試展開一個空組(使用您的代碼)它仍然沒有做任何事情,我可以看到該組正在擴展,因爲我有一個指標,但應該顯示爲空組的文本視圖不顯示出來.. 這是我的適配器:http:// pastebin.com/Yvq5Aveq 我的2佈局(list_item和empty_layout是bassicaly是一樣的,2個線性佈局只有一個textview) –

+0

你有沒有調試代碼,看看調試器是否在正確的開關情況下? –

+0

你是對的,即使是在一個空的組中,它也會進入填充開關的情況 –

0

如果要擴展BaseExpandableListAdapter,你可以改變你BaseExpandableListAdapter的getGroupView換來這取決於他們的孩子是否爲空或不

公衆覆蓋查看GetGroupView(INT groupPosition,布爾isExpanded,查看convertView不同位置的不同看法,ViewGroup中父) {// 爲空的第二個孩子和第二位置 獲得視圖數據,如果(位置== 1 & & _childData.getCount(1)== 0){ // 返回新的TextView,你可以創建一個成員變量textview,並返回 }}

+0

我確實擴展BaseExpandableListAdapter,你能提供一個例子嗎? –