在其他View
中,我尝试以编程方式将ExpandableListView
添加到LinearLayout
。为此,我使用的是SimpleExpandableListAdapter
。应该有十个父元素,每个元素都有一个子元素。如果重要的话,它会被onPostExecute
的{{1}}包围。这是java:
AsyncTask
这是expandable_parent.xml:
ArrayList<HashMap<String,String>> parentList = new ArrayList<>();
ArrayList<ArrayList<HashMap<String,String>>> childList = new ArrayList<>();
for (int k = 0 ; k < 10 ; k++)
{
HashMap<String,String> parentMap = new HashMap<>();
parentMap.put("item", i + ":" + k + "source");
parentList.add(parentMap);
ArrayList<HashMap<String,String>> childItemList = new ArrayList<>();
HashMap<String,String> childMap = new HashMap<>();
childMap.put("item", i + ":" + k + "data");
childItemList.add(childMap);
childList.add(childItemList);
}
/* Build expandable list */
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
details,
parentList,
R.layout.expandable_parent,
new String[] {"item"},
new int[] {R.id.expandable_parent},
childList,
R.layout.expandable_child,
new String[] {"item"},
new int[] {R.id.expandable_child}
);
ExpandableListView info = new ExpandableListView(details);
info.setAdapter(adapter);
layout.addView(info);
并且expandable_child.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/expandable_parent"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
可能存在一些错误,因为我刚刚使用了Java的简化版本。但问题是,当它运行时,只显示第一个父元素。我点击它,左边的图像翻转,表示该组已打开,但没有显示子元素。问题是什么?