根据列表视图的内容对listview中的相似行进行分组

时间:2010-12-22 08:03:16

标签: android listview android-listview

我有一个列表视图,显示一组行,每行都是可点击的。 现在,我希望在一个标题下对相似类型的行进行分组,如图所示(模拟)。有些人可以为此提供建议或提供方法。 mockup image

3 个答案:

答案 0 :(得分:1)

为每个listview项目的“类别”添加额外的参数。然后基于该参数实现类似'StickyListHeaders'的东西。

答案 1 :(得分:0)

我过去曾使用过AmazingListView并且有效。
Google I/O Schedule App建议方法的实现。

我喜欢它的事情:

  1. 粘性标题
  2. 延迟加载的分页
  3. 值得注意的是:

    1. 这是一个基于SVN的项目
    2. 您需要将其作为库包含
    3. 在Android Studio中使用它更难;使用ADT更容易。
    4. I did just post a question about this recently

      这是项目主页上的图片: AmazingListView example lists

答案 2 :(得分:-1)

如果你的适配器是基于Cursor的,那么使用 SectionCursorAdapter 2.0 ,你就不能做到更简单:

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);
        }
    }

}