       
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Platty Soft &#187; code</title>
	<atom:link href="http://www.plattysoft.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.plattysoft.com</link>
	<description>Android consulting fun and professional.</description>
	<lastBuildDate>Mon, 12 Jan 2026 20:55:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.5</generator>
	<item>
		<title>Displaying items in a grid with a header</title>
		<link>http://www.plattysoft.com/2013/07/22/displaying-items-in-a-grid-with-a-header/</link>
		<comments>http://www.plattysoft.com/2013/07/22/displaying-items-in-a-grid-with-a-header/#comments</comments>
		<pubDate>Mon, 22 Jul 2013 07:33:19 +0000</pubDate>
		<dc:creator><![CDATA[Platty Soft]]></dc:creator>
				<category><![CDATA[Appsterdam]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.plattysoft.com/?p=606</guid>
		<description><![CDATA[The problem Let&#8217;s say you want a layout that is a grid of items with a header. Something like this: Displaying items in a grid is easy, we have GridView for that. Displaying a list of items with a header &#8230; <a href="http://www.plattysoft.com/2013/07/22/displaying-items-in-a-grid-with-a-header/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h1>The problem</h1>
<p>Let&#8217;s say you want a layout that is a grid of items with a header. Something like this:</p>
<p style="text-align: center;"><a href="http://www.plattysoft.com/wp-content/uploads/2013/07/framed_device-2013-06-06-153438.png"><img class="aligncenter  wp-image-669" alt="fashiolista profile" src="http://www.plattysoft.com/wp-content/uploads/2013/07/framed_device-2013-06-06-153438-613x1024.png" width="409" height="683" /></a></p>
<p>Displaying items in a grid is easy, we have GridView for that.</p>
<p>Displaying a list of items with a header is easy, we have setHeaderView on ListView for that.</p>
<p>The problem is when we want to show items<strong> in a grid with a header</strong>, since<strong> GridView does not support headers</strong> and <strong>ListView does not support columns</strong>.</p>
<p>This is a common problem and there are a few suggestion on StackOverflow, but none of them goes further than some guidelines. I did implement it and I want to share it so you don&#8217;t need to reinvent the wheel</p>
<p>From the architectural point of view, the solution is to use a ListView with a special adapter that displays the entries as separated columns.</p>
<h1>How the code should look like</h1>
<p>The code at activity level when you configure the view is like this:</p>
<pre class="brush: java; title: ; notranslate">
ListView listView = (ListView) findViewById(R.id.listView);
listView.addHeaderView(createHeaderView());

adapter = new GidViewWithHeaderExampleAdapter(this);
adapter.setNumColumns(2);
listView.setAdapter (adapter);
</pre>
<p>Note that you add the header to the list view as a normal header, but <strong>set the number of  columns to the Adapter</strong>.</p>
<h1>Extending from the right adapter</h1>
<p>The adapter itself has to extend from <em>GridViewWithHeaderBaseAdapter</em> and implement some methods that are slightly different from the ones in a normal adapter.</p>
<pre class="brush: java; title: ; notranslate">
Integer[] mArray = new Integer[] {1,2,3,4,5,6,7,8,9,10,11,12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};

private LayoutInflater mInflater;

public GridViewWithHeaderExampleAdapter(Context context) {
    super(context);
    mInflater = LayoutInflater.from(context);
}

@Override
public Integer getItem(int position) {
    return mArray[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemCount() {
    return mArray.length;
}

@Override
protected View getView(int position, View v) {
    if (v == null) {
        v = mInflater.inflate(R.layout.simple_list_item, null);
    }
    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText(String.valueOf(getItem(position)));
    return v;
}

</pre>
<p>So <strong><em>getItem</em></strong> and <strong><em>getItemId</em></strong> are the same as for normal adapters.</p>
<p>On the other hand <strong><em>getItemCount</em></strong> is a replacement method for getCount and <strong><em>getItemView</em></strong> is a replacement for getView. both are implemented on the <em>GridViewWithHeaderBaseAdapter</em> and are the methods that do the magic.</p>
<h1>Handling click on items</h1>
<p>The adapter creates a LinearLayout and adds child views to it for each row, creating a single list item for each row.<br />
Then, of course, <strong>OnItemClickListener is broken</strong>, because each list item is a row. <strong>You have to use GridItemClickListener instead</strong>.</p>
<pre class="brush: java; title: ; notranslate">
@Override
public void onGridItemClicked(View v, int position, long itemId) {
    // TODO: Handle item click here
}
</pre>
<p><span style="color: #000000; font-weight: bold;">Advantages versus TableLayout</span></p>
<p>What is the advantage of this solution versus just a TableLayout on a ScrollView?</p>
<p>The advantage is that the Adapter recycles the views and it is a lot <strong>more efficient</strong> in memory and in execution.<br />
The other advantage is that the Adapter is dynamic. <strong>You can not hardcode a TableView without knowing the items beforehand</strong>. Sure, you can build it programatically, but if you are getting that far, you probably want to get one step further and build an adapter, which is what I made.</p>
<p><em>GridViewWithHeaderBaseAdapter </em>is about 150 lines of code, so feel free to look at the source for inspiration and/or modify it for your own purposes, it is under BSD license.</p>
<p>You can check <a href="https://github.com/plattysoft/grid-with-header-list-adapter">the project on github</a>. It includes the example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.plattysoft.com/2013/07/22/displaying-items-in-a-grid-with-a-header/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
