<?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 Sultan Seidalin on Medium]]></title>
        <description><![CDATA[Stories by Sultan Seidalin on Medium]]></description>
        <link>https://medium.com/@seidalins?source=rss-39a0ce7dd630------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*T4ymj5CXQM4BW0KAY7uMDw.jpeg</url>
            <title>Stories by Sultan Seidalin on Medium</title>
            <link>https://medium.com/@seidalins?source=rss-39a0ce7dd630------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 07:55:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@seidalins/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[InlineConfigurable: Configuring Objects in Swift the Kotlin Way]]></title>
            <link>https://medium.com/better-programming/inlineconfigurable-configuring-objects-in-swift-the-kotlin-way-e26cc0a1e0d8?source=rss-39a0ce7dd630------2</link>
            <guid isPermaLink="false">https://medium.com/p/e26cc0a1e0d8</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[inline-functions]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sultan Seidalin]]></dc:creator>
            <pubDate>Tue, 03 Mar 2020 09:01:49 GMT</pubDate>
            <atom:updated>2020-03-06T15:29:08.270Z</atom:updated>
            <content:encoded><![CDATA[<h4>A path towards more readable Swift code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xYz7ZVshP2vxRw-rRZX53g.png" /></figure><h3>Background</h3><p>Being both an Android and an iOS developer, I always compare these two platforms and their features. Both have their own advantages and disadvantages, and this makes the development so exciting.</p><p>I usually find myself wondering why some cool features are implemented in one language and missing from another. So in this short article, we’re going to talk about the inline function from Kotlin called apply that we are going to implement in Swift. It helps you configure an object inside one block. You’ll see how it makes your code a lot easier to read.</p><h3>Problem</h3><p>You may be thinking, “Why would we ever need anything from Kotlin in Swift?” However, I’m pretty confident most of you have encountered such a mess in your viewDidLoad()<strong> </strong>method while configuring a UITableView:</p><pre>tableView.delegate = <strong>self<br></strong>tableView.dataSource = <strong>self<br></strong>tableView.tableFooterView = UIView()<br>tableView.addSubview(refreshControl)<br>tableView.separatorStyle = .none<br>tableView.register(// register cell)<br>tableView.register(// register cell)</pre><p>Most of you would think that it’s normal and there’s nothing wrong with this code. I’m on your side, of course — there<em> is </em>nothing wrong with it. But the Android developer inside of me wants to reduce the repetition of the tableView<strong> </strong>variable and put everything inside one block. Let’s see:</p><pre>tableView.apply {<br>    $0.delegate = <strong>self<br>    </strong>$0.dataSource = <strong>self<br>    </strong>$0.tableFooterView = UIView()<br>    $0.addSubview(refreshControl)<br>    $0.separatorStyle = .none<br>    $0.register(// register cell)<br>    $0.register(// register cell)<br>}</pre><p>This looks cleaner and easier to reader, doesn’t it? You can skip the next chapter and go directly into implementation if you don’t want to read the theory behind this implementation.</p><h3><strong>Inline Functions</strong></h3><p>One of the perks of functional programming is the ability to pass a function as a parameter to another function. In Swift. we call this a closure, and in Kotlin, it’s called lambdas.<strong> </strong>Both of them have their own problems.</p><pre>//Kotlin<br>sampleCollection.filter { it == 1 }</pre><pre>//Swift<br>sampleCollection.filter { $0 == 1 }</pre><p>In Swift, the most common problem you’ll encounter with closures is retain cycles that occur when you’re not using them correctly. In order to fix this issue, we use strong, weak, and unowned<strong> </strong>references inside closures.</p><p>In Kotlin, it’s a bit more tricky as it’s still tightly connected to Java. You can read about high-order functions (lambdas) in general <a href="https://www.baeldung.com/kotlin-inline-functions">here</a>. I’m only going to discuss some of the problems that exist.</p><p>The first problem is function-call overhead. If we take the filter function shown above, an additional call to the special method named invoke on the instance is needed in order to actually perform the operation encapsulated in a Kotlin lambda. The result is more overhead due to the extra call.</p><p>Another problem is memory overhead. I’m not going to dive deep, but most of the time, the JVM creates a function-type instance on every invocation. In other words, every time we declare a lambda, a special object of type Function* is created to encapsulate the code inside a lambda — thus generating memory overhead.</p><p>The keyword inline<strong> </strong>in Kotlin comes to the rescue to declare an inline function that fixes both of these issues. When using inline functions, the compiler inlines the function body. That is, it substitutes the body directly into places where the function gets called.</p><pre>val numbers = listOf(1, 2, 3, 4, 5)<br>numbers.each { println(it) }</pre><p>The above gets substituted into the following:</p><pre>val numbers = listOf(1, 2, 3, 4, 5)<br>for (number in numbers) {<br>    println(number)<br>}</pre><p>When using inline functions, there’s no extra object allocation and no extra virtual method calls since instead of handling a lambda, we just extracted the code inside the lambda into the corresponding place.</p><p>You might say, “Nah, I don’t need this inline thing since it’s not present in Swift.” Surprisingly, inline functions exist in Swift as well; however, the @inline keyword is forbidden. It does the same thing as in Kotlin — it replaces the function call with the contents of the function thus reducing additional function-call overhead. Read <a href="https://swiftrocks.com/the-forbidden-inline-attribute-in-swift.html">this article</a> for more information.</p><p>It turns out, there’s another way of working with inline functions in Swift. We may call it a function inside a function or a <em>nested function.</em> Let’s take a look at the example.</p><pre><strong>class</strong> SomeViewController: UIViewController {</pre><pre>    <strong>override</strong> <strong>func</strong> viewDidLoad() {<br>        <strong>super</strong>.viewDidLoad()<br>        <br>        func initHeader() {<br>            //...<br>        }</pre><pre>        func initContent() {<br>            //...<br>        }</pre><pre>        func initFooter() {<br>            //...<br>        }</pre><pre>        initHeader()<br>        initContent()<br>        initFooter()<br>    }<br>}</pre><p>You can find more information about this approach <a href="https://medium.com/@GalvinLi/tinyexperience-when-to-use-inline-function-1c88de4b7ea5">here</a>.</p><h3><strong>Implementation in Swift</strong></h3><p>There are many inline-extension functions in Kotlin, but today we’re going to implement only one called apply — which helps you configure objects inside a block (closure/lambda).</p><p>In Kotlin, apply is an extension function on a particular type that sets its scope to the object on which it is invoked. apply runs on the object reference into the expression and also returns the object reference on completion. It’s not only used to set properties, of course — it’s capable of evaluating complex logic before returning. In the end, it returns the same object, with some modified changes. This is how it looks in Kotlin:</p><pre>inline fun T.apply(block: T.() -&gt; Unit): T {<br>    block()<br>    return this<br>}</pre><p>Now let’s implement it in Swift. It’s quite easy:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e79e0daf793d94e089c00e366a8fcf3/href">https://medium.com/media/9e79e0daf793d94e089c00e366a8fcf3/href</a></iframe><p>What we have done so far:</p><ul><li>We declared the InlineConfigurable<strong> </strong>protocol, which is then implemented by NSObject<strong>. </strong>Other classes can actually also implement this protocol in order to be capable of using it.</li><li>We wrote an extension function called apply<strong> </strong>with a closure called configurator. This closure can have any kind of logic.</li><li>We executed this logic on this object reference</li><li>We returned the object</li></ul><p>Now, you can easily use the function called apply<strong> </strong>wherever you want. For example, you can configure your UIView<strong> </strong>inside one block:</p><pre>containerView.apply {<br>    $0.layer.borderColor = ColorConstants.borderBackground.cgColor<br>    $0.layer.borderWidth = 1<br>    $0.layer.cornerRadius = NumberConstants.cornerRadius<br>    $0.layer.masksToBounds = <strong>true<br></strong>}</pre><p>Any type can implement this protocol in order to benefit from the apply function. However, I found that implementing by NSObject is most of the time enough as it helps you to work with the UIView instances, and this kind of repetitive variable call behavior is often associated with the UIView<strong>.</strong></p><h3><strong>Summary</strong></h3><p>Inline functions, in general, are great when it comes to efficiency and performance.</p><p>However, as we discussed earlier, they’re forbidden in Swift (maybe just for now). Even though the apply<strong> </strong>function implemented in Swift in this article isn’t really an inline function, it helps a lot by reducing the noise in your source files and by making the code a lot more readable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e26cc0a1e0d8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/inlineconfigurable-configuring-objects-in-swift-the-kotlin-way-e26cc0a1e0d8">InlineConfigurable: Configuring Objects in Swift the Kotlin Way</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Delegate Adapters: Building Heterogeneous RecyclerViewAdapter]]></title>
            <link>https://medium.com/@seidalins/delegate-adapters-building-heterogeneous-recyclerviewadapter-877cb7d3c6c0?source=rss-39a0ce7dd630------2</link>
            <guid isPermaLink="false">https://medium.com/p/877cb7d3c6c0</guid>
            <category><![CDATA[composition]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[generic-recycler-view]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Sultan Seidalin]]></dc:creator>
            <pubDate>Thu, 27 Feb 2020 14:01:05 GMT</pubDate>
            <atom:updated>2020-02-28T09:24:39.834Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*CXnj9GwDP-K0ZTEHRs-cjA.png" /></figure><h3><strong>Delegate Adapters: Building Heterogeneous RecyclerView Adapter</strong></h3><h4>An elegant way to eliminate the adapter hell and manage multiple view types even with nested RecyclerViews</h4><h3>Introduction</h3><p>RecyclerView is pretty common widget that has replaced ListView in Android Development long ago and it has brought us a lot of amazing features that are still being developed. I’m not going to dive into basics of ListView and RecyclerView, so let’s assume that we all know what these widgets are created for. Most of the time, we use RecyclerView to display some kind of uniform item view, such as simple text with an icon or something a bit more complex. However, sooner or later you might face a situation where you will be required to implement a RecyclerView with multiple view types.</p><blockquote>Hi, My name is Sultan Seidalin. I am an Android and iOS developer from Kazakhstan. In this article, I want to share how to implement a RecyclerView Adapter which will be able to handle multiple item view types easily.</blockquote><h3>Motivation</h3><p>Once, I received a new task from the product owner and the UI/UX designer. They showed me the design of the new screen which was going to be a part of the upcoming feature.</p><p>The main requirements for this screen:</p><ul><li>It’s a screen which shows your booking and all the information regarding your flight.</li><li>It has a list of cards where each card represents a small part of your flight’s information (e.g passenger information, refund information, etc.).</li><li>Each card has its own functionality (they may have clickable elements inside).</li><li>The order of these cards can be changed according to some future requirements.</li><li>These cards can be shown or hidden according to some conditions.</li><li>Other cards with different information and functionality might be added in the future.</li></ul><p>So, the main property of our implementation should be the ease of modification. We should be able to modify the list of items without needing to rewrite or extensively modify existing code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tcdjzUHueK4l9Qqe" /></figure><p>A great amount of inspiration was taken from <a href="http://hannesdorfmann.com/android/adapter-delegates">“Joe’s great adapter hell escape”</a> by Hannes Dorfmann</p><h3>Conventional way</h3><p>Whenever there is a need to implement multiple view types developers use constants which are used to define each view type. While it’s fine and dandy for couple of items, it quickly becomes a mess when you try to implement five or more view types. What if you need to implement different layouts as shown in the image? Let’s have a look at the conventional way of writing RecyclerView Adapters with multiple view types:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/366f644716c660f64b5d288f5398e99c/href">https://medium.com/media/366f644716c660f64b5d288f5398e99c/href</a></iframe><p>If you continue writing your code in the way shown above, you are in a big trouble. You will end up with a lot of ‘if/else’ or ‘when’ statements which will quickly create a mess in your adapter. Moreover, think about adding one more layout to this adapter. You will have to add one more statement to <strong>onCreateViewHolder()</strong> and <strong>onBindViewHolder()</strong> methods, which breaks the ‘O’ part in SOLID principles that states “Objects or entities should be open for extension, but closed for modification”.</p><blockquote>Anytime you find yourself writing code of the form “if the object is of type T1, then do something, but if it’s of type T2, then do something else,” slap yourself.</blockquote><blockquote>- Danny Preussler</blockquote><p>To solve this problem you need to implement a generic solution that works for any view type without any hazel of ‘if/else’ blocks.</p><h3><strong>An elegant and more efficient way</strong></h3><p>In order to implement such a solution we want to breakdown the task into the smallest possible pieces and solve each of them separately. What if we could have multiple adapters where each of them handles its own view type and one master adapter that binds them all together?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*atirqLyE_EIdkSD0zZRJnQ.jpeg" /></figure><p>That’s what we are going to implement (well, not in the darkness): multiple tiny adapters that can handle their own view type and one composite adapter which will manage all those tiny adapters. I will assume you are familiar with <strong>DiffUtil</strong>, as I am not going to cover it in this article. We will use the term <strong>delegate adapter </strong>instead of tiny adapter in this article from now on.</p><h3>Step 1 — Model</h3><p>First of all, we have to create an interface for all the model classes that will be used in our adapters. Conventionally, you would have created a model class which would be used by ViewHolder to bind to its views. In our case, all the model classes have to implement DelegateAdapterItem interface in order to be a part of our composition.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41f9fadb5840a6070145eb1fd94a8cdc/href">https://medium.com/media/41f9fadb5840a6070145eb1fd94a8cdc/href</a></iframe><p>Here, we created three functions which are used by <strong>DiffUtil </strong>to calculate the difference between items in the list. Additionally, we created a marker interface to be able to create payloads for each item (in order to change only desired part of the ViewHolder without recreating it entirely). At the end, we have a payload value (<strong>Payloadble.None</strong>) which is assigned by default. If we ever need custom payload, we will have to override this method.</p><h3><strong>Step 2 — DiffUtil Callback</strong></h3><p>Implement a generic <strong>DiffUtil</strong> class which works for our DelegateAdapterItem model class as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53fa4ebb9d60f4829e339674821d7bee/href">https://medium.com/media/53fa4ebb9d60f4829e339674821d7bee/href</a></iframe><p>Here, we use <strong>DiffUtil </strong>to check if two items are the same or not and if their contents have changed. If the contents change, then appropriate payload is calculated.</p><h3>Step 3 — Abstract Adapter Class</h3><p>Now that we have our model set up, let’s move on to the delegate adapter. First, we have to create an abstract class which will be extended by our delegate adapters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc476bc5f6367129cb9cf52e126ffab5/href">https://medium.com/media/cc476bc5f6367129cb9cf52e126ffab5/href</a></iframe><p>Basically, it’s a small adapter which uses model class to tell your master adapter that it handles such type of models. You will see how it is used in the following steps, so for now, think of it as <strong>itemViewType</strong>, which is used to distinguish ViewHolders. Functions <strong>createViewHolder()</strong> and <strong>bindViewHolder()</strong> are common, they will be used accordingly. Other functions, such as <strong>onViewRecycled()</strong>, <strong>onViewAttachedToWindow()</strong> and <strong>onViewDetachedFromWindow()</strong> are left open because not every adapter should implement them.</p><h3><strong>Step 4 — Delegate adapter &amp; Model Implementation</strong></h3><p>Let’s create an instance of our delegate adapter with a model class. Our adapter will extend the abstract class shown above and implement corresponding methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00e1d45dcdefb791924f185d806ce89a/href">https://medium.com/media/00e1d45dcdefb791924f185d806ce89a/href</a></iframe><p>Here is our model class which will be used in one of our delegate adapters that will only handle flight information. It looks quite huge, but only because I’ve tried to cover all the possible cases.</p><p>In our case, <strong>bookingId </strong>is used as a return value of <strong>id() </strong>function to show that two models are the same if their <strong>bookingId</strong>s are equal.</p><p>Next, for the <strong>content() </strong>function we created a separate inner class which is used to determine if contents of two items are different. We could have used one of the properties as return value as well, but I tried to cover the complex case, where you would need to check multiple fields or implement complex logic to compare two items.</p><p>In the end, a sealed class which implements <strong>Payloadable </strong>interface is created to have complex payloads. In our case, if departure or arrival time changes, ViewHolder will only change corresponding views without refreshing itself from scratch. Then we use <strong>payload() </strong>function to create appropriate payload object, which is then passed to adapter to update ViewHolder.</p><p>Let’s move on to our adapter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe11bcde2c47529e6a880fe79a4b91a0/href">https://medium.com/media/fe11bcde2c47529e6a880fe79a4b91a0/href</a></iframe><p>Functions <strong>createViewHolder()</strong> and <strong>bindViewHolder() </strong>must be familiar to you, they act the same way as in any other RecyclerView adapter. Inside <strong>bindViewHolder() </strong>I used ‘when’ statement to cover multiple cases. If <strong>DiffUtil </strong>sends us callback that only departure time or arrival time has changed, then <strong>bindDepartureTime() </strong>or <strong>bindArrivalTime() </strong>functions will be called. In all other cases, we will update the entire ViewHolder as we always do.</p><h3>Step 5 — Composite Adapter Implementation</h3><p>Now that we have all our smallest parts implemented (models and adapters) let’s create our composite adapter which binds everything together. Generally, composite adapter is no different from any other <strong>ListAdapter</strong>. But instead of working with a list as usual, it works with a list of adapters and whenever a method is called from <strong>ListAdapter</strong> it passes the responsibilities to the delegate adapter. Let’s have a look at the source code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5ccb4403a01439a09acd34f471c9a1c/href">https://medium.com/media/a5ccb4403a01439a09acd34f471c9a1c/href</a></iframe><p>It extends ListAdapter and uses DelegateAdapterItem as a model. Our <strong>DiffUtil Callback </strong>from Step 2 is passed to ListAdapter’s constructor. Composite adapter has its own constructor that waits for a SparseArray (which is basically a map with key of type <strong>Int</strong>, and an value of type <strong>Object</strong>). Let’s break down our composite adapter:</p><p>First function in our adapter is <strong>getItemViewType()</strong>. Here we can see how our model class, which was mentioned in Step 3, is used to determine which delegate adapter to use. Basically, it loops through all the delegate adapters and checks if there is any delegate adapter that can handle the type of model at given position. If there is one, then it returns a key from the sparse array mentioned above (as <strong>getItemViewType() </strong>has to return <strong>Int </strong>value). If it can’t find any delegate adapter, then exception is thrown saying it cannot get view type for a given position (meaning, there is no delegate adapter that can handle the type of model at a position).</p><p>Next, <strong>onCreateViewHolder() </strong>uses the key returned from <strong>getItemViewType() </strong>to find corresponding delegate adapter and call its <strong>createViewHolder()</strong> function. Function<strong> onBindViewHolder() </strong>acts the same way, it tries to find corresponding delegate adapter and throws exception if no adapter is found.</p><p>Three function at the end <strong>onViewRecycled()</strong>, <strong>onViewAttachedToWindow()</strong> and <strong>onViewDetachedFromWindow()</strong> are implemented optionally. They do not serve any purpose here, I’ve implemented them for display purposes only.</p><p>At the of the file we can see the <strong>Builder</strong> pattern which is used to create our composite adapter. Here we add delegate adapters to our composite adapter and then call <strong>build() </strong>function to create an instance of a composite adapter which is then passed to a RecyclerView. Variable <strong>count</strong> is used as a key and the delegate adapter itself is used as a value in the SparseArray which is then passed to a constructor.</p><h3>Step 6 — Connecting RecyclerView</h3><p>Our solution is now ready to be implemented into our application. What we need to do:</p><ol><li>Create model items.</li><li>Create delegate adapters for each of the models.</li><li>Create an instance of composite adapter and pass delegate adapters to it.</li><li>Connect composite adapter to our RecyclerView.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd4b6007891e6baf79a228d7b49ab70a/href">https://medium.com/media/dd4b6007891e6baf79a228d7b49ab70a/href</a></iframe><p>And that’s basically it. Activity is written for display purposes only and I think it is better to create model items in your <em>ViewModel</em> and then pass it to the <em>Activity</em> or <em>Fragment</em> using <em>LiveData&lt;DelegateAdapterItem&gt;</em>, but this is for you to implement. :)</p><p>In case if you need to change the order of the ViewHolders, just change the order of the model items in the list. Whenever you need to add other types of layouts, just create one more model, create corresponding delegate adapter and add it to composite adapter using <strong>add() </strong>function. It is quite easy and we don’t have to touch any previously created adapters.</p><h3>Nested RecyclerViews</h3><p>The delegate adapters we have created here are powerful and generic enough to work with nested RecyclerViews without any changes in the adapter. Surprisingly, we can have a list of items inside our model class and create regular adapter inside our delegate adapter to handle this list. This way, we can have nested RecyclerViews inside ViewHolders.</p><p>Let’s consider example of such complex list with nested RecyclerViews. We will take Google Play Store as an example. It has many different view types and has nested RecyclerViews inside some of them. Firstly, we need to break down this screenshot to understand what it consists of. From the top to bottom:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/263/1*UwVkuPthxLk8oZWusywdKg.jpeg" /></figure><ol><li>Tab bar — just a regular TabBarLayout.</li><li>Banners— list of swipeable cards. Might be a ViewPager or RecyclerView.</li><li>List of Car Games — swipeable list of car games with a title. TextView with an Arrow Button on top and RecyclerView at the bottom.</li><li>Rating — rating view with an app name and a title.</li><li>List of Non-stop action — same as item 3, but with different title and different list of games.</li></ol><p>Now, let me try to show you how you could implement such screen.</p><p>We can clearly see, that we will have three view types and three adapters for each of them respectively:</p><ul><li>BannerAdapterItem + BannerDelegateAdapter</li><li>GameListAdapterItem + GameListDelegateAdapter</li><li>RatingAdapterItem + RatingDelegateAdapter</li></ul><p>Let’s have a look at the code, I will omit some details as this is just to show you how easy and intuitive it is to implement delegate adapters.</p><p>Code for Banner Item</p><pre>data class BannerAdapterItem(<br>    bannerList: List&lt;Banner&gt;<br>): DelegateAdapterItem {<br>    //...<br>}</pre><pre>class BannerDelegateAdapter(<br>    private val onBannerClicked: ((Banner) -&gt; Unit)<br>): DelegateAdapter&lt;BannerAdapterItem, ViewHolder&gt;(BannerAdapterItem::class.java) {<br>    //...<br>}</pre><p>Code for Game List Item</p><pre>data class GameListAdapterItem(<br>    title: String,<br>    games: List&lt;Game&gt;<br>): DelegateAdapterItem {<br>    //...<br>}</pre><pre>class GameListDelegateAdapter(<br>    private val onGameClicked: ((Game) -&gt; Unit)<br>): DelegateAdapter&lt;GameListAdapterItem, ViewHolder&gt;(GameListAdapterItem::class.java) {<br>    //...<br>}</pre><p>Code for Rating Item</p><pre>data class RatingAdapterItem(<br>    game: Game<br>): DelegateAdapterItem {<br>    //...<br>}</pre><pre>class RatingDelegateAdapter(<br>    private val onRatingClicked: ((Int) -&gt; Unit)<br>): DelegateAdapter&lt;RatingAdapterItem, ViewHolder&gt;(RatingAdapterItem::class.java) {<br>    //...<br>}</pre><p>Finally, code for your activity</p><pre>class MainActivity: AppCompatActivity {</pre><pre>    private val compositeAdapter by lazy {<br>        CompositeAdapter.Builder()<br>            .add(BannerAdapter(...))<br>            .add(GameListDelegateAdapter(...))<br>            .add(RatingDelegateAdapter(...))<br>            .build()<br>    }</pre><pre>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)</pre><pre>        val bannerItem = BannerAdapterItem(...)<br>        val gameListItem = GameListAdapterItem(...)<br>        val ratingItem = RatingAdapterItem(...)<br>        val listOfItems = listOf&lt;DelegateAdapterItem&gt;(<br>            bannerItem, gameListItem, ratingItem<br>        )</pre><pre>        compositeAdapter.submitList(listOfItems)<br>    }<br>}</pre><h3><strong>Summary</strong></h3><p>Let’s sum up what we have done here. We created several tiny adapters that will only handle one type of model, this corresponds to S.R.P principle of SOLID. We created composite adapter which stores these delegate adapters and in case if we need to add one more layout, we just add one more adapter. This conforms to Open-Closed principle of SOLID.</p><p>By separating the adapters based on view type, we achieved cleaner and more readable code, not to mention the ease of modification and expansion. I think that it’s a quite elegant solution which will help you decompose your code and write more complex RecyclerViews.</p><p>Check out my <a href="https://github.com/Sultan1993/EasyDelegateAdapter">Github Repo</a> which includes simple example of delegate adapter pattern implementation.</p><p>You can find me on <a href="https://medium.com/@seidalins">Medium</a> and <a href="https://www.linkedin.com/in/sultan-seidalin-543b177b/">LinkedIn</a>.</p><blockquote>If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment below.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=877cb7d3c6c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>