我的任务是在过滤期间保持展开/折叠的组。我编写了自己的过滤器,如果组没有任何孩子,则删除组。但是在组消失后,过滤器的文本被更改并且组返回,但它会被折叠。
所以我保存展开/折叠状态来分组对象。所以我只需要在适当的时候应用这些状态。
有我的尝试:
class SubjectGroupList extends ExpandableListView implements OnGroupCollapseListener, OnGroupExpandListener{
public SubjectGroupList(Context context) {
super(context);
}
public void onGroupExpand(int groupPos) {
SubjectGroup group = (SubjectGroup)getExpandableListAdapter().getGroup(groupPos);
group.expanded = true;
}
public void onGroupCollapse(int groupPos) {
SubjectGroup group = (SubjectGroup)getExpandableListAdapter().getGroup(groupPos);
group.expanded = false;
}
@Override
public void onFilterComplete(int count) {
fixCollapse();
super.onFilterComplete(count);
}
private void fixCollapse() {
Log.d("SubjectGroupList", "fix collapse");
SubjectGroupAdapter adapter = (SubjectGroupAdapter)getExpandableListAdapter();
int i = adapter.getGroupCount();
while (--i >= 0)
if (adapter.getGroup(i).expanded)
expandGroup(i);
else
collapseGroup(i);
}
}
但这根本不起作用。永远不会调用onFilterComplete方法。
我必须找到另一种方法来确定,在重新绘制之前何时应用过滤器。
也许有人知道?谢谢。
编辑:
我的过滤器:
class GroupFilter extends Filter {
private SubjectGroupAdapter adapter;
public GroupFilter(SubjectGroupAdapter adapter) {
this.adapter = adapter;
}
@Override
protected FilterResults performFiltering(CharSequence s) {
FilterResults result = new FilterResults();
result.count = 0;
ArrayList<SubjectGroup> groups = new ArrayList<SubjectGroup>();
s = s.toString().toLowerCase();
for (SubjectGroup group : adapter.ori) {
SubjectGroup ng = new SubjectGroup(group.name, group.id);
int founded = 0;
for (SubjectItem item : group) {
if (s == null || s.length() == 0 || item.name.toLowerCase().contains(s)) {
ng.add(new SubjectItem(item.name, item.id));
result.count++;
founded++;
}
}
if (founded > 0)
groups.add(ng);
}
result.values = groups;
return result;
}
@Override
protected void publishResults(CharSequence s, FilterResults result) {
adapter.filter((ArrayList<SubjectGroup>) result.values);
}
}
*以及接受过滤数据的适配器方法 * :
protected void filter(ArrayList<SubjectGroup> groups) {
this.groups = groups;
notifyDataSetInvalidated();
}