11

SectionIndexerの代わりにを使用する方法を見つけようとしていますAlphabetIndexer。私が興味を持っているのは、アルファベットの代わりにセクション ヘッダーに文字列配列の要素を含めることです。セクション インデクサーを使用したサンプル コードは見つかりませんでした。

のサンプル コードは次のAlphabetIndexerとおりです。

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

stringArray「ABCDEFGHIJKLMNOPQRSTUVWXYZ」の代わりにa を渡すことは可能ですか?AlphabetIndexerたとえば、「A」、「B」、...「Z」の代わりにヘッダーとして「Book」、「Food」、...ヘッダー?

そうでない場合、それを行う最良の方法は何ですか?SectionIndexer代わりにを使用するサンプル コードへの参照AlphabetIndexerも役立ちます。

助けてくれてありがとう。TJ

4

1 に答える 1

12

カスタムを作成して、基本的に、ヘッダーを表示する位置のメソッドで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クラスは、タイトル、サブタイトル、およびそれがセクションヘッダーであるかどうかを判断するブール値の単純なコンテナーです。

このアダプターを作成する方法はこれだけではありませんが、これがお役に立てば幸いです。

于 2011-07-28T16:54:32.080 に答える