【问题标题】:Android: how to use SectionIndexerAndroid:如何使用 SectionIndexer
【发布时间】:2011-10-15 05:40:37
【问题描述】:

我正在尝试找到一种方法来使用SectionIndexer,而不是AlphabetIndexer。我感兴趣的是在节标题而不是字母表上有字符串数组的元素。我无法使用节索引器找到任何示例代码。

这是AlphabetIndexer的示例代码:

private AlphabetIndexer indexer;
indexer = new AlphabetIndexer(c, c.getColumnIndexOrThrow(
   DbHelper.COUNTRIES_NAME),"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

是否可以将 stringArray 而不是 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 传递给 AlphabetIndexer 所以我可以例如代替 "A"、"B"、..."Z",因为标题有 "Book", “食物”,……在标题上?

如果不是,最好的方法是什么?对使用SectionIndexer 而不是AlphabetIndexer 的示例代码的任何引用也会有所帮助。

感谢您的帮助。 TJ

【问题讨论】:

  • 您只是想在 ListView 中分离元素还是有其他原因要使用 SectionIndexer?
  • 我想在我的列表视图中创建部分,每个部分都有自己的标题,每个部分都有许多可点击的元素。当然部分标题不应该是可点击的。
  • 我在这里做类似的事情stackoverflow.com/questions/10224233/…

标签: android listview header indexer


【解决方案1】:

您可以尝试编写一个自定义的ArrayAdapter 并在getView(...) 方法中为标题应该出现的位置返回一个“节标题”视图。

您还必须覆盖 getViewTypeCount () 以返回新视图类型的数量(在本例中为 2)和 getItemViewType (int position) 以返回当前位置的视图类型。

另外,onItemClickListener 应该检查您单击的项目是否是节标题。

这是我的自定义数组适配器:

public class ItemListAdapter extends ArrayAdapter<ModelItem>
{
    private static final int    TYPE_SECTION_HEADER = 0;
    private static final int    TYPE_LIST_ITEM  = 1;

    int mDefaultRowLayoutResID;
    Context mContext;
    LayoutInflater mInflater;
    ArrayList<ModelItem> lItems;

    public ItemListAdapter(Context context, int resource, ArrayList<ModelItem> items)
    {
        super(context, resource, items);
        mContext = context;
        mResource = resource;
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        lItems = items;
    }

    @Override
    public ModelItem getItem(int position)
    {
        return lItems.get(position);
    }

    @Override
    public int getCount()
    {
        return lItems.size();
    }

    @Override
    public int getViewTypeCount()
    {
        return 2;
    }

    @Override
    public int getItemViewType(int position)
    {
        ModelItem item = lItems.get(position);
        if (item.isHeader())
        {
            return TYPE_SECTION_HEADER;
        }
        else
        {
            return TYPE_LIST_ITEM;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        ModelItem item = getItem(position);

        if (convertView == null)
        {
            if (item.isHeader())
            {
                convertView = mInflater.inflate(R.layout.row_item_section_header, null);
                holder = new ViewHolder();
                holder.title = (TextView)convertView.findViewById(R.id.list_header_title);
                holder.subtitle = null;
                convertView.setTag(holder);
            }
            else
            {
                convertView = mInflater.inflate(R.layout.row_item_default, null);
                holder = new ViewHolder();
                holder.title = (TextView)convertView.findViewById(R.id.row_item_title);
                holder.subtitle = (TextView)convertView.findViewById(R.id.row_item_subtitle);
                convertView.setTag(holder);
            }
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(item.getTitle());
        if (holder.subtitle != null)
        {
            holder.subtitle.setText(item.getSubtitle());
        }
        return convertView;
    }

    private class ViewHolder
    {
        public TextView title;
        public TextView subtitle;
        public ImageView leftIcon;
        public View rightControl;
    }
}

这是 row_item_default.xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/row_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <TextView
        android:id="@+id/row_item_subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/row_item_title"
    />
</RelativeLayout>

这是 row_item_section_header.xml:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_header_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="?android:attr/listSeparatorTextViewStyle"
/>

ModelItem 类是标题、副标题和布尔值的简单容器,用于判断它是否是节标题。

这不是编写此适配器的唯一方法,但我希望这会有所帮助。

【讨论】:

  • 我正在尝试使用这些代码,但在接受答案之前我有一些问题。 @Mircea 再次感谢您的帮助:在我之前的代码中,我有一个 String Arrat“文件”,我使用它的方式是:'setListAdapter(new ArrayAdapter(this, R.layout.row, files));' .现在使用您的代码我应该使用 ItemListAdapter 而不是 ArrayAdapter?使用它的正确方法是什么?我应该有:'setListAdapter(new ItemListAdapter (this, R.layout.row, files));' ?
  • 您可以像setListAdapter(new ItemListAdapter &lt;ModelItem&gt;(this, R.layout.row, modelItems)); 一样使用它,但是您必须首先从files 的数组中构建ArrayList&lt;ModelItem&gt; modelItems,然后在您的节标题应该插入的位置插入其他项目。这个想法是您的 ItemListAdapter 知道在哪里将行作为节标题膨胀,以及在哪里将其作为普通行膨胀。我通过创建 ModelItem 类并使其以 true 响应 isHeader() 来做到这一点,其中节标题应该是,而对于普通行则为 false。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多