[英]Collapse all group except selected group in expandable listview android
I'm developing android application using expandable list view. 我正在使用可扩展列表视图开发android应用程序。 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.
请帮帮我。
Have the current expanded group position stored in a variable. 将当前扩展的组位置存储在变量中。 In onGroupExpanded do the following.
在onGroupExpanded中执行以下操作。
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;
}
});
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;
}
});
@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;
}
If the ExpandableListView has more than 2 groups: 如果ExpandableListView有两个以上的组:
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. 除了点击的组之外,它将折叠所有组。
Get ExpendableListView
and then override the following method - setOnGroupExpandListener
获取
ExpendableListView
,然后重写以下方法 - 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;
}
});
Try putting this in your ExpandableListAdapter, listView is a reference to the ExpandableListView itself. 尝试将它放在ExpandableListAdapter中,listView是对ExpandableListView本身的引用。 And lastExpandedGroupPosition is a integer member variable defined inside your ExpandableListAdapter.
lastExpandedGroupPosition是在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;
}
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);
}
}
}
});
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 因为我在可扩展列表中有4个Headers,基于我这样写的
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);
}
});`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.