我想一次只扩展父列表中的一个项目,我的onCreate代码目前如下. (它可以按我的意愿工作,但是一次只打开一个父项的方法不起作用)
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,我使用expandablelist的布局将我的程序保存到我的.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活动?
list在类中定义为私有的ExpandableListView列表;
我现在没有任何错误,但该方法不起作用.
任何帮助是极大的赞赏!
解决方法
// Declare variable
private static int prev = -1;
// and OnGroupExpandListener implement
mExpandableList.setonGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if(prev!=-1)
{
mExpandableList.collapseGroup(prev);
}
prev=groupPosition;
}
});
更新更新的解决方案
mExpandableList.setonGroupExpandListener(new OnGroupExpandListener() {
// Keep track of prevIoUs expanded parent
int prevIoUsGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
// Collapse prevIoUs parent if expanded.
if ((prevIoUsGroup != -1) && (groupPosition != prevIoUsGroup)) {
mExpandableList.collapseGroup(prevIoUsGroup);
}
prevIoUsGroup = groupPosition;
}
});