2013-01-19 48 views
0

有沒有辦法在同一個列表視圖中顯示可擴展列表和不可擴展列表? 我試圖實現的是有可擴展的列表視圖,但對於沒有子項目的項目,我不想顯示箭頭展開箭頭圖標 我該如何做到這一點?請幫助 感謝如何顯示可擴展列表視圖和單列表視圖

編輯:所有我想要的是能夠隱藏指示箭頭當組爲空

回答

1

1)基本上,當組沒有孩子時,你必須隱藏「展開箭頭」。 您可以通過將GroupIndicator屬性設置爲null來實現此目的。

對於前:getExpandableListView().setGroupIndicator(null);

OR

2)通過檢查孩子們指望你可以設置如下面的代碼的指標,

if (getChildrenCount(groupPosition) == 0) { 
     indicator.setVisibility(View.INVISIBLE); 
    } else { 
     indicator.setVisibility(View.VISIBLE); 
     indicator.setImageResource(isExpanded ? R.drawable.list_group_expanded : R.drawable.list_group_closed); 
    } 

有關隱藏組指標的詳細信息,你可以參考this link

OR

3)創建繪製文件夾中的文件group_indicator.xml

<item android:state_empty="true" android:drawable="@android:color/transparent"></item> 
<item android:state_expanded="true" android:drawable="@drawable/arrowdown"></item> 
<item android:drawable="@drawable/arrowright"></item> 

然後,定義如下面的代碼擴展列表視圖,

<ExpandableListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:groupIndicator="@drawable/group_indicator" 
     android:layout_weight="1" > 
    </ExpandableListView> 

希望這將解決你的問題。

+0

這確實是我正在尋找的。最後一個建議不會工作,因爲它總是處於空狀態,這是由android的限制(實際上他們說這是出於性能原因) – Snake

+0

但是,在您的第一個建議,將其設置爲null將禁用整個列表 – Snake

1

是。您可以。

使用BaseExpandableListAdapter

這個類有下一個方法: getGroupTypeCount()和getGroupType(INT groupPosition)

,你應該重寫。

以簡單的方式,你有兩種類型的項目。例如可擴展(類型1)和不可擴展(類型2)。

getGroupTypeCount()shoul return 2;

重寫getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent) 在此方法中按項目的位置檢查項目的類型。 如果item不可擴展,您應該生成View,它的外觀與擴展與否無關。

我覺得這很簡單。

+0

謝謝,不確定我是否在getgroupview方面得到了重點,並且一個示例會非常有用 – Snake