Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can try to write a custom <code>ArrayAdapter</code> and basically return a "section header" view in the <code>getView(...)</code> method for the positions where headers should appear.</p> <p>You'll also have to overwrite <code>getViewTypeCount ()</code> to return the number of new types of views (in this case 2) and <code>getItemViewType (int position)</code> to return the type of view for the current position.</p> <p>Also, the <code>onItemClickListener</code> should check to see if the item you clicked on is a section header.</p> <p>This is my custom array adapter:</p> <pre><code>public class ItemListAdapter extends ArrayAdapter&lt;ModelItem&gt; { private static final int TYPE_SECTION_HEADER = 0; private static final int TYPE_LIST_ITEM = 1; int mDefaultRowLayoutResID; Context mContext; LayoutInflater mInflater; ArrayList&lt;ModelItem&gt; lItems; public ItemListAdapter(Context context, int resource, ArrayList&lt;ModelItem&gt; 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; } } </code></pre> <p>This is the row_item_default.xml file:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;TextView android:id="@+id/row_item_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/row_item_subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/row_item_title" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>and this is the row_item_section_header.xml:</p> <pre><code>&lt;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" /&gt; </code></pre> <p>The ModelItem class is a simple container for title, subtitle and a boolean to tell if it's a section header or not.</p> <p>This is not the only way to write this adapter but I hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload