1.声明变量
//声明记录停止滚动时候,可见的位置
private int scrollPos,scrollTop;
//记录是否展开
private HashMap<Integer, Boolean> gxmap = new HashMap<Integer, Boolean>();
2.默认展开
if(subBaseList.size()>0)
{
//设置adapter
mAdapter = new EqStateExAdapter(context, subBaseList);
lvState.setAdapter(mAdapter);
}
for (int i = 0; i < mAdapter.getGroupCount(); i++)
{
//默认记录为已展开
gxmap.put(i, true);
//全部展开
lvState.expandGroup(i);
}
3.listview回调中处理记录
lvState.setOnScrollListener(new AbsListView.OnScrollListener()
{
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
//滑动时记录当前可见的位置
scrollPos = lvState.getFirstVisiblePosition();
View v1 = lvState.getChildAt(0);
scrollTop = (v1 == null) ? 0 : v1.getTop();
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)
{}
});
lvState.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener()
{ //关闭监听
@Override
public void onGroupCollapse(int groupPosition)
{
gxmap.put(groupPosition, false);//记录
}
});
lvState.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
{ //展开监听
@Override
public void onGroupExpand(int groupPosition)
{
gxmap.put(groupPosition, true);//记录
}
});
4.数据更新后刷新listview
for (int i = 0; i < mAdapter.getGroupCount(); i++)
{
if(gxmap.size()>0)
{
//根据记录展开或关闭
Boolean isgx = gxmap.get(i);
if (isgx)
{
lvState.expandGroup(i);
}
else
{
lvState.collapseGroup(i);
}
}else{
//默认展开
lvState.expandGroup(i);
}
}
记录.