这是我最近对ExpandableListView 所做的事情。这相当容易。
它拥有迄今为止我在 android 中见过的最大的构造函数之一。
使用ExpandableListView 创建一个布局。
<LinearLayout>
<ExpandableListView>
<!-- Set the properties, if you need to see them, let me know. -->
</ExpandableListView>
</LinearLayout>
现在在某些视图中使用布局。
public void onCreate() {
ExpandableListView example = findViewById(/* The id of the expandable list view */);
example.setAdapter(
new SimpleExpandableListAdapter(
/* the context */,
/* the map of parents/groups */,
/* the layout to map the parents/groups to */,
/* the key to search for in the parents/groups map */,
/* the id of the textview inside the layout to map the parent/group to */,
/* the map of children */,
/* the layout to map the children to */,
/* the key to search for in the children map */,
/* the id of the textview inside the layout to map the children to */)
)
}
还有其他可用的适配器,但这是最基本的。
基本上只看developer website。这个example 也有点帮助,但是开发者的网站还有很长的路要走。
如果您有任何问题,请告诉我。
跟进
他放了 createGroupList()。看起来这是他评论的功能
出去。这到底是在做什么?
他基本上是在该函数中创建HashMaps 的List 并返回创建的对象。是的,您应该这样做,它有助于使您的代码更简洁。
此外,这些列表显示得很小,无论如何要让它们变大?
修改您的TextView,以显示您的孩子和组。在 xml 布局中为 View 添加填充。只需调整dp。您可以增加textSize 或者您可以添加style,如下所示。
<TextView
android:id="@+id/groupTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Medium"
android:paddingLeft="50dp"
android:paddingTop="25dp"
android:paddingBottom="25dp"/>
我希望父组是列表,但不希望子组
是子列表,我希望它们成为我可以添加文本的文本框(我
从网上抢)。我如何制作儿童文本框而不是
列表。
您可能需要针对这种情况创建自己的Adapter。您需要扩展BaseExpandableListAdapter。我可能是错的,可能有更简单的方法......但我不知道。
额外的跟进
不确定你的意思。在子列表中输入的文本
出现在这行代码中:child.put("Sub Item", "test")。我能
把我想要的文字放在写着“测试”但“测试”出现在下面的地方
每个父列表。
您的createChildList 需要返回一个List<ArrayList<HashMap<String, String>>>。
您的createChildList 应该如下所示:
private List<ArrayList<HashMap<String, String>>> createChildList(int maxValue) {
ArrayList<ArrayList<HashMap<String, String>>> groupList =
new ArrayList<ArrayList<HashMap<String, String>>>();
for (int i = 0; i <= maxValue; i++) {
ArrayList<HashMap<String, String>> childList =
new ArrayList<HashMap<String, String>>();
for (int j = 0; j <= maxValue; j++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Sub Item", "test: " + String.valueOf(j));
childList.add(map);
}
groupList.add(childList);
}
return groupList;
}
让我知道这是否有效。