<?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 Yura Terletskyi on Medium]]></title>
        <description><![CDATA[Stories by Yura Terletskyi on Medium]]></description>
        <link>https://medium.com/@yterletskyi?source=rss-bc7005a1b2a6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*3yOz_saYHr69fNvH23e_1g.jpeg</url>
            <title>Stories by Yura Terletskyi on Medium</title>
            <link>https://medium.com/@yterletskyi?source=rss-bc7005a1b2a6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:31:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yterletskyi/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[How RecyclerView.Adapter breaks Open-Closed Principle and what we can do about it]]></title>
            <link>https://medium.com/@yterletskyi/how-recyclerview-adapter-breaks-open-closed-principle-and-what-we-can-do-about-it-21761ced2e43?source=rss-bc7005a1b2a6------2</link>
            <guid isPermaLink="false">https://medium.com/p/21761ced2e43</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[open-closed-principle]]></category>
            <category><![CDATA[open-close-principle]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[solid]]></category>
            <dc:creator><![CDATA[Yura Terletskyi]]></dc:creator>
            <pubDate>Tue, 17 Jan 2023 21:08:50 GMT</pubDate>
            <atom:updated>2023-01-17T21:08:50.329Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CeAX5Ldu-rEnH_XT_CQ5aw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@philberndt?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Phil</a> on <a href="https://unsplash.com/photos/5i0GnoTTjSE?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Recent days I had to go through SOLID principles again in Nth time to refresh them in my memory. It doesn’t hurt I though. Now, few days later I woke up with the idea that Android RecyclerView.Adapter actually violates the OCP! Let me explain.</p><h3>What does Open-Closed Principle mean?</h3><p>As per <a href="https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle">wikipedia</a>, OCP states that:</p><blockquote>Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.</blockquote><p>Which in turn implies that when adding a new feature we should not modify (well, at least <em>minimize </em>modifying) existing files.</p><h3>How does RecyclerView.Adapter break the OCP?</h3><p>Now, recall what we do every time when adding a new view type to a RecyclerView. We do:</p><ol><li>modify fun getItemViewType(position: Int): Int</li><li>modify fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder</li><li>modify fun onBindViewHolder(holder: ExampleViewHolder, position: Int)</li></ol><p>Looks like a red flag — doesn’t it?</p><h3>What can we do about it?</h3><p>My idea is to split the single big adapter into multiple ones by view types. In other words — delegate ‘adapting’ every view type to a separate class. Then, have a singleRecyclerView.Adapter to deal all the delegates and expose them to a RecyclerView .</p><p>Following the above approach the top-level interface to a RecyclerView should be simplified to the next:</p><pre>recyclerView.adapter = RecyclerDelegationAdapter()<br>    .apply {<br>        addDelegates(<br>            SampleTextDelegate1(),<br>            SampleTextDelegate2(),<br>            ImageDelegate(),<br>        )<br>        setItems(items)<br>    }</pre><p>I have implemented the prototype on my github and am welcoming you to check it out: <a href="https://github.com/yterletskyi/DelegationRecyclerViewAdapter">https://github.com/yterletskyi/DelegationRecyclerViewAdapter</a></p><p>The code is only a proof-of-concept and has a lot of space for improvements. I’d be glad if you share your opinions and feedbacks!</p><h4>Long Story Short (The code)</h4><pre>interface ModelItem</pre><pre>import android.view.View<br>import android.view.ViewGroup<br>import androidx.recyclerview.widget.RecyclerView<br><br>abstract class AdapterDelegate&lt;VH : AdapterDelegate.Holder&gt; {<br><br>    open class Holder(view: View) : RecyclerView.ViewHolder(view)<br><br>    abstract fun onCreateViewHolder(parent: ViewGroup): VH<br><br>    abstract fun onBindViewHolder(viewHolder: VH, item: ModelItem)<br><br>    abstract fun isForViewType(item: ModelItem, position: Int): Boolean<br><br>    abstract fun getViewType(): Int<br>}</pre><pre>import android.util.SparseArray<br>import android.view.ViewGroup<br>import androidx.recyclerview.widget.RecyclerView<br><br>class RecyclerDelegationAdapter : RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;() {<br><br>    private val delegates: MutableList&lt;AdapterDelegate&lt;AdapterDelegate.Holder&gt;&gt; = ArrayList()<br>    private val itemTypeToDelegatesMap = SparseArray&lt;AdapterDelegate&lt;AdapterDelegate.Holder&gt;&gt;()<br>    private val items: MutableList&lt;ModelItem&gt; = ArrayList()<br><br>    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {<br>        val delegate = itemTypeToDelegatesMap[viewType]<br>        return delegate.onCreateViewHolder(parent)<br>    }<br><br>    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {<br>        val delegate = itemTypeToDelegatesMap[holder.itemViewType]<br>        delegate.onBindViewHolder(holder as AdapterDelegate.Holder, items[position])<br>    }<br><br>    override fun getItemViewType(position: Int): Int {<br>        val item = items[position]<br>        val delegate = delegates<br>            .find { delegate -&gt; delegate.isForViewType(item, position) }<br>            ?: throw NullPointerException(&quot;Delegate not found for item at position: $position&quot;)<br><br>        val viewType = delegate.getViewType()<br>        itemTypeToDelegatesMap[viewType] = delegate<br>        return viewType<br>    }<br><br>    override fun getItemCount(): Int = items.size<br><br>    fun setItems(items: List&lt;ModelItem&gt;) {<br>        this.items.clear()<br>        this.items.addAll(items)<br>        notifyDataSetChanged()<br>    }<br><br>    fun &lt;D : AdapterDelegate&lt;*&gt;&gt; addDelegate(delegate: D) {<br>        delegates.add(delegate as AdapterDelegate&lt;AdapterDelegate.Holder&gt;)<br>    }<br>}</pre><pre>class MainActivity : AppCompatActivity() {<br><br>    private val recyclerView: RecyclerView by lazy {<br>        findViewById(R.id.recyclerView)<br>    }<br><br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        setContentView(R.layout.activity_main)<br><br>        recyclerView.adapter = RecyclerDelegationAdapter()<br>            .apply {<br>                addDelegate(<br>                    SampleTextDelegate1(),<br>                )<br>                addDelegate(<br>                    SampleTextDelegate2()<br>                )<br>                setItems(<br>                    (0..10).map {<br>                        if (it % 2 == 0) TextModelItem1(&quot;view type 1&quot;)<br>                        else TextModelItem2(&quot;view type&quot;, &quot;2&quot;)<br>                    }<br>                )<br>            }<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21761ced2e43" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>