【问题标题】:group similar rows in listview based on their content根据内容对列表视图中的相似行进行分组
【发布时间】:2010-12-22 08:03:16
【问题描述】:

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

【问题讨论】:

  • 您目前使用的是哪种适配器?您对如何显示“分隔符”列表项感兴趣吗?
  • +1 便于理解的绘画
  • 我想执行同样的任务,但所有的值都会在运行时更新。请建议我如何做到这一点,我尝试了很多但得到重复的部分列表。总之没有成功。

标签: android listview android-listview


【解决方案1】:

为每个列表视图项的“类别”添加一个额外的参数。然后基于该参数实现类似“StickyListHeaders”的东西。

【讨论】:

    【解决方案2】:

    我过去使用过AmazingListView,效果不错。
    它是Google I/O Schedule App 建议方法的实现。

    我喜欢它的地方:

    1. 粘性标题
    2. 延迟加载分页

    注意:

    1. 这是一个基于 SVN 的项目
    2. 您需要将其作为库包含
    3. Android Studio 使用起来比较困难;使用 ADT 更容易。

    (I did just post a question about this recently)

    这是来自项目主页的图片:

    【讨论】:

      【解决方案3】:

      如果你的适配器是基于游标的,那么使用 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);
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-09
        • 2018-10-27
        • 2015-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多