问题
i have a listview that displays a set of rows, each row is clickable. now, i wish to group similar type of rows under one header something like as shown in the figure (mocked up). could some please advise or provide an approach for this.
回答1:
Add an extra paremeter for the 'category' of each listview item. Then implement something like 'StickyListHeaders' based on that parameter.
回答2:
I've used AmazingListView in the past with some effectiveness.
It is an implementation of the Google I/O Schedule App suggested approach.
Things that I like about it:
- Sticky headers
- Pagination with lazy loading
Of note:
- It's an SVN-based project
- You need to include it as a library
- It's harder to use it with Android Studio; much easier with ADT.
(I did just post a question about this recently)
Here's an image from the project home page:
回答3:
If your adapter is Cursor based then use SectionCursorAdapter 2.0, you can't do simplier:
public class MyAdapter extends SectionCursorAdapter<String, MyAdapter.SectionViewHolder, MyAdapter.ItemViewHolder> {
public MyAdapter(Context context, Cursor cursor) {
super(context, cursor, 0, R.layout.item_section, R.layout.item_title);
}
// this method will fullfill your wishes
@Override protected String getSectionFromCursor(Cursor cursor) {
return cursor.getString(cursor.getColumnIndexOrThrow("group"));
}
// replace getView/bindView/newView
// --------------------------------------------
@Override protected SectionViewHolder createSectionViewHolder(View sectionView, String section) {
return new SectionViewHolder(sectionView);
}
@Override protected ItemViewHolder createItemViewHolder(Cursor cursor, View itemView) {
return new ItemViewHolder(itemView);
}
@Override protected void bindSectionViewHolder(int position, SectionViewHolder sectionViewHolder, ViewGroup parent, String section) {
sectionViewHolder.titleView.setText(section);
}
@Override protected void bindItemViewHolder(ItemViewHolder itemViewHolder, Cursor cursor, ViewGroup parent) {
itemViewHolder.title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
itemViewHolder.titleView.setText(itemViewHolder.text);
}
// view holders
// --------------------------------------------
public class SectionViewHolder extends ViewHolder {
public TextView titleView;
public SectionViewHolder(View rootView) {
super(rootView);
titleView = findWidgetById(R.id.sectionText);
}
}
public class ItemViewHolder extends ViewHolder {
public String title;
public TextView titleView;
public ItemViewHolder(View rootView) {
super(rootView);
titleView = findWidgetById(R.id.titleText);
}
}
}
来源:https://stackoverflow.com/questions/4507248/group-similar-rows-in-listview-based-on-their-content