Android:如何使用 SectionIndexer [英] Android: how to use SectionIndexer

查看:24
本文介绍了Android:如何使用 SectionIndexer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来使用 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.

这是 AlphabetIndexer 的示例代码:

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

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

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?

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

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.

感谢您的帮助.TJ

推荐答案

你可以尝试编写一个自定义的 ArrayAdapter 并在 getView(...) 标题应该出现的位置的方法.

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.

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

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.

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

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

这是 row_item_default.xml 文件:

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>

这是 row_item_section_header.xml:

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

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

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.

这篇关于Android:如何使用 SectionIndexer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆