Android:如何使用SectionIndexer

发布于 2024-11-26 17:03:30 字数 632 浏览 5 评论 0原文

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

以下是 AlphabetIndexer 的示例代码:

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

是否可以将 stringArray 而不是“ABCDEFGHIJKLMNOPQRSTUVWXYZ”传递给 AlphabetIndexer,这样我就可以代替“A”,“B”,...“Z”作为标题,标题上有“Book”,“Food”,...?

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

感谢您的帮助。 泰杰

I am trying to find a way to use SectionIndexer, instead of AlphabetIndexer. What I am interested to do is to have elements of a string arrays on the section headers instead of alphabets. I have not been able to find any sample code using section indexer.

Here is a sample code for AlphabetIndexer:

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

Is it possible to pass a stringArray instead of "ABCDEFGHIJKLMNOPQRSTUVWXYZ" to the AlphabetIndexer so I can for example instead of "A", "B", ..."Z" as header have "Book", "Food", ...on the headers?

If not what is the best way to do that? Any reference to a sample code that uses SectionIndexer instead of AlphabetIndexer would be helpful as well.

Thanks for the help.
TJ

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烛影斜 2024-12-03 17:03:30

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

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

此外,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 类是一个简单的标题、副标题和布尔值容器,用于判断它是否是节标题。

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

You can try to write a custom ArrayAdapter and basically return a "section header" view in the getView(...) method for the positions where headers should appear.

You'll also have to overwrite getViewTypeCount () to return the number of new types of views (in this case 2) and getItemViewType (int position) to return the type of view for the current position.

Also, the onItemClickListener should check to see if the item you clicked on is a section header.

This is my custom array adapter:

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

This is the row_item_default.xml file:

<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>

and this is the 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"
/>

The ModelItem class is a simple container for title, subtitle and a boolean to tell if it's a section header or not.

This is not the only way to write this adapter but I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文