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

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

问题描述

我想,而不是 AlphabetIndexer 来找到一种方法,使用 SectionIndexer 。我有兴趣做的是​​有一个字符串数组的段头,而不是字母元素。我一直没能找到c。使用部分索引任何样品$ C $。

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.

下面是一个简单的$ C $下 AlphabetIndexer

Here is a sample code for AlphabetIndexer:

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

而不是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 将是有益的,以及样品code

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

Thanks for the help. 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位置)返回类型来看当前的位置。

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天全站免登陆