我已经实施了StickyListHeaders,但它并没有按照我的意愿行事。标题似乎没有正确分组。请参见下图,每个(折叠)标题的图片。
正如您所见,标题2和4是相同的。它们具有相同的ID,但未按预期分组。
以下是我的适配器代码。 (为了便于阅读,删除了与视图相关的代码)
public class PartyMemberArrayAdapter extends ArrayAdapter<PartyMember> implements View.OnClickListener, StickyListHeadersAdapter {
private ArrayList<PartyMember> members;
public PartyMemberArrayAdapter(Context c, ArrayList<PartyMember> members) {
super(c, R.layout.list_item_party_member, members);
this.members = members;
}
@Override
public View getHeaderView(int i, View view, ViewGroup viewGroup) {
HeaderViewHolder holder;
View row;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
holder = new HeaderViewHolder();
row = inflater.inflate(R.layout.list_header_party_member, viewGroup, false);
holder.party = (TextView) row.findViewById(R.id.tv_list_header_party);
row.setTag(holder);
} else {
row = view;
holder = (HeaderViewHolder) view.getTag();
}
holder.party.setText(PartyUtil.getFullPartyNameFromAbbr(members.get(i).getParty()));
return row;
}
@Override
public long getHeaderId(int i) {
//There's 8 different parties, each with 8 names with unique length as I use for ID.
return PartyUtil.getFullPartyNameFromAbbr(members.get(i).getParty()).length();
}
private class HeaderViewHolder{
TextView party;
}
@Override
public int getCount() {
return members.size();
}
@Override
public PartyMember getItem(int position) {
return members.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
我在这里遗漏了什么吗?仅仅为标题返回一个唯一ID才能正确分组是不够的?
答案 0 :(得分:0)
通过实现排序算法解决了这个问题,该算法根据它们“属于”哪个标题对基础数据集进行排序。
Collections.sort(members, new Comparator<PartyMember>() {
public int compare(PartyMember p1, PartyMember p2) {
return p1.getParty().compareToIgnoreCase(p2.getParty());
}
});