Android:如何使子列表可选的列表元素?

时间:2013-04-02 11:09:02

标签: android android-layout android-listview

我有一个ListView,其元素具有包含其元素的子列表。 这看起来像这样:

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 个答案:

答案 0 :(得分:1)

使用expandable lists

可以解决您的需求问题

try this guide for expandable lists

您还可以通过使用ArrayAdapter

扩展课程来尝试使用自己的自定义适配器
Override the method
public View getView(int position, View convertView, ViewGroup parent) 

在子列表中添加事件。