<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cláudio Ribeiro on Medium]]></title>
        <description><![CDATA[Stories by Cláudio Ribeiro on Medium]]></description>
        <link>https://medium.com/@claudiorpr?source=rss-2acd61b3d786------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-BPwIsUtCFQwfhKr4y3PeA.jpeg</url>
            <title>Stories by Cláudio Ribeiro on Medium</title>
            <link>https://medium.com/@claudiorpr?source=rss-2acd61b3d786------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:40:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@claudiorpr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android Adapters — A different approach]]></title>
            <link>https://medium.com/@claudiorpr/android-adapters-a-different-approach-54379aa3a3f6?source=rss-2acd61b3d786------2</link>
            <guid isPermaLink="false">https://medium.com/p/54379aa3a3f6</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Cláudio Ribeiro]]></dc:creator>
            <pubDate>Thu, 25 Aug 2016 12:21:10 GMT</pubDate>
            <atom:updated>2016-08-25T12:21:10.878Z</atom:updated>
            <content:encoded><![CDATA[<p>Building a list of items on Android is very simple. We set up a RecyclerView, bind an adapter, throw in some items with their ViewHolders and magic happens.</p><p>Everything works and we’re all happy. Well… as you can imagine… I’m not happy at all :D and that’s the reason why I wrote this post.</p><h3><strong>The Monster adapter</strong></h3><p>The following code is an example of a working list adapter. It supports three types of items: a header with a configurable label, a simple contact item that displays name and email and some dummy object with two labels.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9cb232e51fdd4e91417bcb1814f3f33/href">https://medium.com/media/d9cb232e51fdd4e91417bcb1814f3f33/href</a></iframe><p>I think that all Android developers made something like this at least once and it’s the kind of example that I see all over the place in tutorials. I would like to highlight some problems with this approach:</p><ul><li>The adapter should be used only to <strong>display </strong>items in a list. It shouldn’t have to know anything about <strong>how to render a specific item</strong>. This is being done on the <em>bindSeparator</em>, <em>bindContact</em> and <em>bindDummy</em> methods. It definitely shouldn’t be here.</li><li>The logic of loading the xml layout to inject it in a <em>ViewHolder</em> is in the adapter. Again this isn’t the <em>Adapter</em> responsibility.</li><li>The <em>ViewHolders</em> are bounded to the adapter. It can be improved to allow reusability.</li></ul><p>A simple way of fixing this is by splitting the adapter in smaller components and delegate responsibility. So my approach is to create three base components: <strong>BaseAdapter</strong>, <strong>ViewHolderManager</strong> and <strong>BaseViewHolder</strong>.</p><p>The <strong>BaseAdapter</strong> is responsible for receiving a list of items and displaying them. It uses a <strong>ViewHolderManager</strong> for building the item specific <strong>ViewHolder</strong>. This is where I apply the xml layout selection logic.</p><p>The<strong> ViewHolder</strong> is able to render itself. It includes assigning click listeners to holder elements, setting data on textviews, fetching images with Picasso/Glide, etc.</p><p>So let’s see some code.</p><h3><strong>BaseViewHolder — with great power comes great responsibility</strong></h3><p>First, I removed the <em>ViewHolders</em> from the adapter class since I didn’t want to bind them to any specific adapter.</p><p>I started by creating an abstract <em>BaseViewHolder</em> which is responsible for binding the click listener. More common functionality can be placed in this class. Every <em>ViewHolder</em> extending it must implement it’s own render logic, including view elements binding (you can use ButterKnife) and setting data from model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25778ada925e56b0d42abfda3eefc026/href">https://medium.com/media/25778ada925e56b0d42abfda3eefc026/href</a></iframe><h3>ViewHolderManager — The new sheriff in town</h3><p>Now the <strong>ViewHolderManager</strong>. It’s an interface that defines all methods needed to build an item <em>ViewHolder</em> and bind the xml layout to it. I use a different manager for each adapter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e69645a0121fc8c73c3d15b90525f0b4/href">https://medium.com/media/e69645a0121fc8c73c3d15b90525f0b4/href</a></iframe><p>The class <strong>MultiItemViewHolderManager</strong> is a <em>ViewHolderManager</em> implementation that evaluates the <em>ViewType</em> and inflates the corresponding xml layout. It then returns a fully loaded specific <em>ViewHolder</em>.</p><p>For simplicity, in this example the choice depends only on a <em>ViewHolder</em> type that is evaluated. Normally I use more complex logic divided through other components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97aa1b4a263040d79109eefb0a6d3b10/href">https://medium.com/media/97aa1b4a263040d79109eefb0a6d3b10/href</a></iframe><h3>BaseAdapter — I know nothing</h3><p>Finally the adapter. The <strong>BaseAdapter</strong> implements all common methods using generics. It delegates the view build logic to the <em>ViewHolderManager</em> and the bind data logic to the <em>ViewHolder</em> itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4202b418238a0b85fb5141a8f47317c/href">https://medium.com/media/c4202b418238a0b85fb5141a8f47317c/href</a></iframe><p>The class <strong>MultiItemAdapter</strong> is an example of an adapter that allows multiple item types and supports click events. Much simpler than the typical monster adapter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f71f28afc73e8f40db4b9974747c78b7/href">https://medium.com/media/f71f28afc73e8f40db4b9974747c78b7/href</a></iframe><h3><strong>Do you have a working sample code?</strong></h3><p>Yes I do :D You can check a full example of this code <a href="https://github.com/crpr/android-adapters">here</a>. There’s an example of a Monster adapter, a simple adapter with only one item type and a multi item type adapter.</p><p>My next post will be about how to inject dependencies into the adapter (using Dagger2), adding swipe to dismiss and reusing ViewHolders as item templates.</p><p>And that’s all, thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54379aa3a3f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>