我正在使用可扩展列表视图开发 android 应用程序。实际上我需要的是,我正在列出包含孩子的组。
如果我选择了一个不可展开的组,它应该展开,在我选择第二个组之后,第一个组应该被折叠。我做了谷歌,但我找不到我想要的。请帮帮我。
我正在使用可扩展列表视图开发 android 应用程序。实际上我需要的是,我正在列出包含孩子的组。
如果我选择了一个不可展开的组,它应该展开,在我选择第二个组之后,第一个组应该被折叠。我做了谷歌,但我找不到我想要的。请帮帮我。
将当前展开的组位置存储在变量中。在 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;
}
});
使用此代码,这将起作用
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;
}
如果 ExpandableListView 有 2 个以上的组:
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);
}
}
}
});
它将折叠除单击的组之外的所有组。
获取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;
}
});
试着把它放在你的 ExpandableListAdapter 中,listView 是对 ExpandableListView 本身的引用。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);
}
}
}
});
我也面临同样的问题。但我可以通过以下代码解决我的问题:
因为我的可扩展列表中有 4 个标题,基于我这样写
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);
}
});`