2013-04-02 64 views
0

我有一個ListView元素有一個子元素與他們的元素。 這看起來是這樣的:Android:如何使列表元素可選子列表?

enter image description here

我的問題是這樣的:

所有的元素應該是可選擇的。但是,只要沒有爲此元素存儲任何子列表(請參閱具有綠色背景色的元素'Abc'),此功能就可以工作。 我怎樣才能使選擇其他元素(那些與子列表,在這種情況下「Do Br」或「X Y」)?注意:元素名稱中的空白不重要。

代碼:

主要列表:

<ListView 
    android:id="@+id/listViewWithSubLists" 
    android:choiceMode="singleChoice" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</ListView> 

進入主目錄:

<TextView 
    android:id="@+id/listentry" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#004400" 
    android:textSize="18sp" /> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:paddingLeft="20dip" 
    > 
    <ListView 
     android:id="@+id/sublistView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

回答

1

您requir EMENT問題可以通過使用expandable lists

try this guide for expandable lists

您也可以嘗試使用自己定製的適配器通過擴展類來解決與ArrayAdapter

Override the method 
public View getView(int position, View convertView, ViewGroup parent) 

添加事件在子列表。

+0

感謝您的提示。我已經考慮過使用它,但是這有一個缺點,就是你沒有看到子列表的頂部元素而沒有點擊任何東西。這是我的主要目標之一。 – Mayoares

+0

從onResume方法的ExpandableListView嘗試此方法 public boolean expandGroup(int groupPos); http://developer.android.com/reference/android/widget/ExpandableListView.html#expandGroup (int) – anurag

相關問題