可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm developing android application using expandable list view. Actually what I need is, I'm listing group, which contains child.
If I select an unexpandable group, it should expand, after I ll select second group at that time the first group should be collapsed. I did Google, but I couldn't find what I want. Please help me out.
回答1:
Have the current expanded group position stored in a variable. In onGroupExpanded do the following.
private int lastExpandedPosition = -1;
private ExpandableListView lv; //your expandable listview
...
lv.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
lv.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
回答2:
Use this code this will work
expandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
int previousItem = -1;
@Override
public void onGroupExpand(int groupPosition) {
if(groupPosition != previousItem )
expandableList.collapseGroup(previousItem );
previousItem = groupPosition;
}
});
回答3:
@Override
public void onGroupExpanded(int groupPosition){
//collapse the old expanded group, if not the same
//as new group to expand
if(groupPosition != lastExpandedGroupPosition){
listView.collapseGroup(lastExpandedGroupPosition);
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
回答4:
If the ExpandableListView has more than 2 groups:
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
for (int g = 0; g < expandableListAdapter.getGroupCount(); g++) {
if (g != groupPosition) {
expandableListView.collapseGroup(g);
}
}
}
});
It will collapse all groups except the clicked one.
回答5:
Get ExpendableListView
and then override the following method - setOnGroupExpandListener
expandableListView = (ExpandableListView) findViewById(R.id.exp_listview);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousItem = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousItem)
expandableListView.collapseGroup(previousItem);
previousItem = groupPosition;
}
});
回答6:
Try putting this in your ExpandableListAdapter, listView is a reference to the ExpandableListView itself. And lastExpandedGroupPosition is a integer member variable defined inside your ExpandableListAdapter.
@Override
public void onGroupExpanded(int groupPosition)
{
//collapse the old expanded group, if not the same
//as new group to expand
if(groupPosition != lastExpandedGroupPosition)
{
listView.collapseGroup(lastExpandedGroupPosition);
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
回答7:
elstListView1.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
for(int i=0;i<listDataHeader.size();i++){
if(i==groupPosition){
//do nothing}
else{
elstListView1.collapseGroup(i);
}
}
}
});
回答8:
I also faced the same problem. But I could solved my problem by the following code:
As I had 4 Headers in my expandable list, based on that i wrote like this
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPos) {
// TODO Auto-generated method stub
expListView.collapseGroup(groupPos+1);
expListView.collapseGroup(groupPos-1);
expListView.collapseGroup(groupPos-2);
expListView.collapseGroup(groupPos+2);
expListView.collapseGroup(groupPos+3);
expListView.collapseGroup(groupPos-3);
}
});`