我想在父列表中只做一个项目,一次展开,我的onCreate代码当前是下面的代码。 (它的工作原理,因为我想,但该方法只允许一位家长在时间不工作打开)Android中的ExpandableLists,我想只允许一个父列表一次展开
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
//FOR LEVEL 1 SCREEN
this,
createGroupList(), // METHOD TO CREATE PARENTLIST Creating group List.
R.layout.group_row, // REF TO XML FILENAME.
new String[] { "Group Item" }, // STRING ARRAY HOLDING ONE KEY THAT REF'S ALL PARENT LIST ITEMS.
new int[] { R.id.row_name }, // REF TO I.D. OF PARENT XML. ID of each group item.-Data under the key goes into this TextView.
createChildList(), // METHOD TO CREATE CHILD LST. childData describes second-level entries.
R.layout.child_row, // XMLFILE NAME FOR CHILD LIST.Layout for sub-level entries(second level).
new String[] {"Sub Item"}, // KEY FOR CHILD ITEMS IN HASHMAP. Keys in childData maps to display.
new int[] { R.id.grp_child} // XML ID FOR TEXTVIEW. Data under the keys above go into these TextViews.
);
setListAdapter(expListAdapter); // setting the adapter in the list.
//**THIS IS WHAT I DONT KNOW HOW TO CODE:**
list = (ExpandableListView) findViewById(R.id.row_name);
list.setAdapter(expListAdapter);
list.setGroupIndicator(null);
list.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
int len = expListAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
list.collapseGroup(i);
}
}
}
});
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
我明白了上面的代码,但是,我不使用expandableListViews,林refferncing我的计划,我的.XML文件与可扩展列表的布局。所以,我不知道如何将上述适用于我的计划......
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bkg"/>
<TextView android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items"/>
</LinearLayout>
,我不知道如何运用的方法如下:
list = (ExpandableListView) findViewById(R.id.row_name);
list.setAdapter(expListAdapter);
list.setGroupIndicator(null);
list.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
int len = expListAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
list.collapseGroup(i);
}
}
}
});
1)我如何将“列表”重新引用到我的expandablelist活动中?
列表中的类定义为private ExpandableListView list;
我没有任何错误的权利,但该方法是行不通的。
任何帮助,非常感谢!
我仍然停留在这之后.. – user1180937 2012-02-06 17:34:47
请问有没有什么办法,我可以碰到这到列表的顶部? – user1180937 2012-02-08 15:16:54
你可以在下面的链接找到解决方案: - http://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview – 2012-11-27 07:51:22