group similar rows in listview based on their content

蓝咒 提交于 2019-12-12 08:18:10

问题


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:

  1. Sticky headers
  2. Pagination with lazy loading

Of note:

  1. It's an SVN-based project
  2. You need to include it as a library
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!