<?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 Can Önal on Medium]]></title>
        <description><![CDATA[Stories by Can Önal on Medium]]></description>
        <link>https://medium.com/@onalcan?source=rss-fba0139bf8ff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JVhEA4WTJV_cSBPpl9Dgkw.jpeg</url>
            <title>Stories by Can Önal on Medium</title>
            <link>https://medium.com/@onalcan?source=rss-fba0139bf8ff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:33:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@onalcan/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[RecyclerView with Multiple Views in Kotlin]]></title>
            <link>https://blog.devgenius.io/recyclerview-with-multiple-views-in-kotlin-bffe299c1994?source=rss-fba0139bf8ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/bffe299c1994</guid>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[recyclerview-kotlin]]></category>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[viewtype]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Can Önal]]></dc:creator>
            <pubDate>Sun, 23 Jan 2022 15:17:22 GMT</pubDate>
            <atom:updated>2022-02-07T08:37:33.678Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M5CEQopXe1Z4M42W4juZYA.jpeg" /></figure><p>In the first part of my recyclerview article, I showed how to create a simple recyclerview from scratch. Now, I will demonstrate how to add a different item layout to the same recyclerview with the same adapter. Since I will build on my previous project, you might want to <a href="https://blog.devgenius.io/android-kotlin-recyclerview-with-databinding-788d353a67bf">take a look at it</a>.</p><p>Let’s dive deep into the code.</p><p>In the previous project, I had a model class named LargeNews . Now, I add another class named ShortNews . Considering OOP principles it is a good practice to have an abstract parent class named News , which holds the blueprints of any news type. Any class that inherits News has to have a title and an image and these properties should be overridden by them. Thus, we define News and its properties as abstract. Additionally, ShortNews takes a date and LargeNews takes a description. Although it is not meant for real-life use, it helps us to see the difference more clearly.</p><pre>abstract class News {<br>    abstract val title: String<br>    abstract val image: Drawable?<br>}</pre><pre>data class ShortNews(<br>    override val title: String,<br>    override val image: Drawable?,<br>    val date: String<br>) : News()</pre><pre>data class LargeNews(<br>    override val title: String,<br>    override val image: Drawable?,<br>    val description: String<br>) : News()</pre><p>In DummyData there is a function that creates and returns News objects as a list which is consist of LargeNews and ShortNews . Since News is the parent of LargeNews and ShortNews, we can return them to the same list as News. Obviously, items will be displayed on the screen according to the order we add them to the list.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1729a61b830a5868f9bb3aa1a1c1af7a/href">https://medium.com/media/1729a61b830a5868f9bb3aa1a1c1af7a/href</a></iframe><p>The next step is to prepare the layout for ShortNews items. To save time, I will create a really basic and unattractive UI. So, create a new layout file and customize it as you want. <a href="https://blog.devgenius.io/android-kotlin-recyclerview-with-databinding-788d353a67bf">As I demonstrate in the first part of the article</a>, convert the file to a data binding layout, if you would like to use DataBinding. I add a textview to the same item_large_news.xml file. And my item_short_news.xml file looks like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/824cb65c8c1d3ba61b5afb68b79b92f3/href">https://medium.com/media/824cb65c8c1d3ba61b5afb68b79b92f3/href</a></iframe><p>Now, it is time to create viewholder class for the ShortNews . It is almost the same with the LargeNewsViewHolder except for the naming. Inside the viewholder, there is a function that binds the shortNews object to the layout’s shortNews variable. It is possible to define different functions and behaviors than LargeNewsViewHolder.</p><pre>class ShortNewsViewHolder(<br>    private val binding: ItemShortNewsBinding<br>) : RecyclerView.ViewHolder(binding.<em>root</em>) {<br>    fun bind(shortNews: ShortNews) {<br>        binding.<em>shortNews </em>= shortNews<br>    }<br>}</pre><p>Thereafter, it is time to arrange the NewsAdapter that has all the logic for different view types, according to our needs.</p><p>NewsAdapter , from the previous project, takes LargeNewsViewHolder as the view holder. However, this doesn’t work anymore because we have another view holder, namely ShortNewsViewHolder . The adapter must be compatible with both view holder types. To provide that, use RecyclerView.ViewHolder a class because both view holders inherit from RecyclerView.ViewHolder . Just as the case with LargeNews, ShortNews , and News.</p><p>Subsequently, start by changing the list’s type from LargeNews to News because the list consists of LargeNews and ShortNews and both of them inherit from News as I have explained already.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*NOwhdxjUCEhCHPmW7M-ifg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*9QG0yHPGHhz61ahXMg131A.png" /></figure><p>Since viewholder type has changed, IDE tells us to override the functions. Then, add the binding classes for both item layouts inside the class.</p><pre>private lateinit var bindingLargeNews: ItemLargeNewsBinding<br>private lateinit var bindingShortNews: ItemShortNewsBinding</pre><p>When you look at the onCreateViewHolder function, you see that the function returns RecyclerView.ViewHolder . So, we will use the when structure to return either LargeNewViewHolder or ShortNewViewHolder . And to differentiate between which view is currently drawn, we will use the viewType parameter of the function. This parameter will return an integer value according to the current item’s type. To handle this behavior, override getItemViewType function.</p><p>Before implementing the function, to keep things in order, create an enum class named NewsType that holds the possible news types with a value. This value will be used as the viewType. Nevertheless, you can still give this value without using the enum class.</p><pre>enum class NewsType(val value: Int) {<br>    <em>LARGE_NEWS</em>(1), <em>SHORT_NEWS</em>(2)<br>}</pre><p>In the getItemViewType get the current item and check its type by the is keyword. If it is a type of ShortNews , then return the value we defined in the NewsType which is 2. onCreateViewHolder will use this value to check the view type. And the same applies to the LargeNews . Also, we must write the else case and I prefer to put LargeNews in it. But, this will not make any difference since we only have two types. As an another option you can throw an exception for else case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*xJ1x9s3PJvSv7sdQzwGKxw.png" /></figure><p>Now in the onCreateViewHolder, by checking the value of the viewType that we recently set, we will inflate the view’s layout and return an instance of it. Again, we must write the else block, and I will fill it by LargeNews. Or as mentioned, you can throw an exception.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vs1tb_IrdcbB33nl9qVtaw.png" /></figure><p>In the onBindViewHolder function, we will call the bind function of the viewholder. In order to decide which viewholder’s bind function to call, again check the current item’s type. To use the holder parameter as the desired viewholder type, use the as keyword and convert it. If the current item is a type of ShortNews bind the item as ShortNews and the same applies to LargeNews .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fTodA4dP0QYsH7_kQEhUvg.png" /></figure><p>Lastly, getItemCount returns the size of the list.</p><p>The final step is to change the variable names in the MainActivity.kt to keep things clear. But no adding or removing code is necessary if you followed my first article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yB_ZoG_Hjzm9OXpPjNg8oQ.png" /></figure><p>And that is all. Although it doesn’t look pretty, it is working fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ccKk6fTqCt8A45rQKmSV_A.gif" /></figure><p>You can check the complete project <a href="https://github.com/canonall/MultiView-RecyclerView">here</a>.</p><p>Any feedback or suggestions are highly appreciated!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bffe299c1994" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/recyclerview-with-multiple-views-in-kotlin-bffe299c1994">RecyclerView with Multiple Views in Kotlin</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Kotlin RecyclerView with DataBinding]]></title>
            <link>https://blog.devgenius.io/android-kotlin-recyclerview-with-databinding-788d353a67bf?source=rss-fba0139bf8ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/788d353a67bf</guid>
            <category><![CDATA[recyclerview-in-android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-data-binding]]></category>
            <dc:creator><![CDATA[Can Önal]]></dc:creator>
            <pubDate>Sat, 08 Jan 2022 13:15:53 GMT</pubDate>
            <atom:updated>2022-01-23T15:42:40.290Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*6KK-0PwmQYroYiGnwqfLiw.png" /></figure><p>In this article, I will show you how to implement a r<a href="https://developer.android.com/guide/topics/ui/layout/recyclerview">ecyclerview</a>, which is an essential component for Android Framework, from scratch in the context of a News application. I will also use the <a href="https://developer.android.com/topic/libraries/data-binding">Android DataBinding library</a> in this simple project. Then, I will explain some key points, which can be interview questions, about it such as its difference from <a href="https://developer.android.com/reference/android/widget/ListView">ListView</a>. In order to keep it simple, I will not focus on the architecture and UI of the app.</p><p>Let’s jump into the code without further ado.</p><p>Right after creating an empty Android project, the first thing we will do is to add the necessary build feature and plug-in to the Module’s build.gradlefile for DataBinding. Add the following build feature inside android{} brackets.</p><pre>buildFeatures<strong>{<br>    </strong>dataBinding true<br><strong>}</strong></pre><p>Then, don’t forget to add the necessary plug-in to plugins{} brackets.</p><pre>id &#39;kotlin-kapt&#39;</pre><p>I would like to create a News app. For that, I will create LargeNewsclass. In the <a href="https://medium.com/@onalcan/recyclerview-with-multiple-views-in-kotlin-bffe299c1994">next series</a> of this article, I will create ShortNews class using a different layout but a single adapter. That is the reason why I name it LargeNewsin this article. <a href="https://medium.com/@onalcan/recyclerview-with-multiple-views-in-kotlin-bffe299c1994">You can check it out, if you would like to see a recyclerview that contains multiple view types.</a> Otherwise, don’t mind it and just continue.</p><p>Add a new data class named LargeNews. It will take two parameters: a title and an image.</p><pre>data class LargeNews(<br>    val title: String,<br>    val image: Drawable?<br>)</pre><p>The next step is to add a recyclerview to your activity or fragment. For simplicity, I will add it directly to MainActivity.kt. Open activity_main.xml and add recyclerview inside your parent layout. Here, click to the left edge of the file where you find &lt;?xml…?&gt; tag. There you will see a light bulb(or press alt+enter) and select “convert to data binding layout”. Or manually, wrap your layout inside&lt;layout&gt;..&lt;/layout&gt; tags and xmls rows inside the first layout tag. We will repeat this for also other layout files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/1*1BspVHUOKn_Y_onhMHTRkw.png" /></figure><p>After, XML file looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ba41db9d7a14fe79bd3473db7d0946f/href">https://medium.com/media/9ba41db9d7a14fe79bd3473db7d0946f/href</a></iframe><p>Next, we should design what each of our recyclerview items looks like. As I have mentioned before, I won’t spend much time on design. However, you can be as creative as you want. But try to avoid creating deep layout hierarchies and adding long text to TextView.</p><p>Inside the layout package, add a new layout file. First, convert your layout file to a data binding layout file and then organize the layout. In my layout, there will be an image and below this image, there will be the title for the news. Here, the neat part is adding a data binding variable. Inside &lt;data&gt;...&lt;/data&gt; tags, we create a variable. The type<em> </em>attribute of this variable will be our package_name.<em>class_name</em> of the model class. For the name attribute, you are free to choose but the class name makes sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*5uc7ztPesKTK7Vl53uUodw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*_X4XpddCLR-6pqI2yoo9dA.png" /><figcaption>type should be package_name.class_name</figcaption></figure><p>Subsequently, we can use the variable name to add LargeNews parameters to desired view’s attributes.</p><pre>//Backgroud to ImageView<br>android:background=&quot;@{largeNews.image}&quot;<br>or<br>//Text to TextView<br>android:text=&quot;@{largeNews.title}&quot;</pre><p>Final XML file looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cd1c6211a51e848c978247aee1937ad/href">https://medium.com/media/8cd1c6211a51e848c978247aee1937ad/href</a></iframe><p>Since we determined the layout, we can start implementing the adapter and the viewholder. As stated in the official documents:</p><blockquote>“The ViewHolder is a wrapper around a View that contains the layout for an individual item in the list. The Adapter creates ViewHolder objects as needed, and also sets the data for those views.”</blockquote><p>You can write the viewholder class inside the adapter class. Nevertheless, I like separating them. We create LargeNewsViewHolder class and extend RecyclerView.Viewholder. To use data binding we get ItemLargeNewsBinding as a parameter. We write a function called bind that takes a LargeNews object as a parameter. This function binds this object to the layout.</p><pre>class LargeNewsViewHolder(<br>    private val binding: ItemLargeNewsBinding<br>) : RecyclerView.ViewHolder(binding.<em>root</em>) {<br>    fun bind(largeNews: LargeNews) {<br>        binding.<em>largeNews </em>= largeNews<br>    }<br>}</pre><p>On the other hand, the adapter class must extend from RecyclerView.Adapter&lt;<em>your_view_holder_class_name</em>&gt; class. At this point, you will see that you need to override three functions. Also, NewsAdapter should take a list containing LargeNews objects. This list will be sent from MainActivity.kt .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*YQoK6iviPZ2SH15Ei0v83w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*vqu0lrho9uHRLATMANLCMg.png" /></figure><ul><li>onCreateViewHolder: creates new item layouts by inflating the layout.</li><li>onBindViewHolder: binds the data to the related position.</li><li>getItemCount: returns the size of the list.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cb82c2566ffe999591ff3f3d60ef889/href">https://medium.com/media/9cb82c2566ffe999591ff3f3d60ef889/href</a></iframe><p>Finally, in MainActivity.kt we get the data from getDummyDatafunction. Then we create an adapter object and give this adapter to recyclerview. Also, recyclerview needs a layout manager. Here we can choose from three options: <a href="https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager">LinearLayoutManager</a>, <a href="https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager">GridLayoutManager</a>, <a href="https://developer.android.com/reference/androidx/recyclerview/widget/StaggeredGridLayoutManager">StaggeredGridLayoutManager</a> . Each of them aims for a different design and should be used accordingly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50ebfa158b953b6db410641e46eba2a6/href">https://medium.com/media/50ebfa158b953b6db410641e46eba2a6/href</a></iframe><p>At the end when we run the app we will see something like that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/180/1*vyz46MtqAOS9Ztr17e3hyQ.gif" /></figure><p>I know it is not appealing at all but yet it is working…</p><h3>Additional Information About RecyclerView</h3><p>One possible interview question about recyclerview is its difference from the <a href="https://developer.android.com/reference/android/widget/ListView">listview</a>. Before recyclerview, Android world was creating list-based screens with listview. However, listview is not a modern and performant way of doing it. Because listview loads all the items in the list at once to the screen although some of them aren’t visible on the screen. Whereas, recyclerview loads items that are visible on the screen. Therefore, it takes less memory and becomes efficient.</p><p>We can see this behavior of recyclerview using Android Studio’s layout inspector tool. To do that, simply run your app and open the layout inspector. Looking at the gif below, you can follow the items by looking at the title number. In total, I have seven items. When we are on the top of the list, we see that only the first three items are loaded. Then, we scroll so that item 1 is not visible anymore. At this point, we can observe that item 1 is removed from the layout and item 4 is added instead. And this is how it keeps going.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e3EfaCVoCw4-1jGGokYhKA.gif" /><figcaption>only visible items are added to layout</figcaption></figure><p>Nonetheless, if you would like to take the advantage of recyclerview’s this behavior, you should be careful not to put recyclerview inside a <a href="https://developer.android.com/reference/androidx/core/widget/NestedScrollView">NestedScrollView</a> in XML. In order to show you that, I will change my activity_main.xml as below.</p><pre>&lt;androidx.core.widget.NestedScrollView<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;&gt;<br><br>    &lt;androidx.recyclerview.widget.RecyclerView<br>        android:id=&quot;@+id/rvNews&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        app:layout_constraintEnd_toEndOf=&quot;parent&quot;<br>        app:layout_constraintStart_toStartOf=&quot;parent&quot;<br>        app:layout_constraintTop_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>&lt;/androidx.core.widget.NestedScrollView&gt;</pre><p>When we run the app and open the layout inspector, we will see that all 7 items are loaded to the layout despite the fact that only 3 of them are visible on the screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DwfgTIch_rJuO2t0BUP1tw.png" /></figure><p>So, if you are going to use recyclerview inside a scrollable parent, be aware of the fact that you are not benefiting from its “recycle” feature. Therefore, it becomes less efficient. The reason for that is, normally, a recyclerview loads as many items as they fit on the screen height. However, when a recyclerview has a scrollable parent, regardless of the screen height it will create all the items at once since its parent height is infinite.</p><p>Another thing we can mention about increasing recyclerview’s performance is setHasFixedSize(boolean hasFixedSize) function. If you know in advance that recyclerview’s size(width and height) is not affected by the adapter contents, you can sethasFixedSize it to true. For example, if you have an image in your item and its size is not changing(the same applies to other elements) then you should use</p><pre>recyclerViewNews.setHasFixedSize(true)</pre><p>Moreover, if you would like to use a divider for each item, you can use ItemDecoration it instead of view to increase the performance.</p><pre>recyclerViewNews.addItemDecoration(<br>    DividerItemDecoration(<br>        this,<br>        LinearLayoutManager.<em>VERTICAL<br>    </em>)<br>)</pre><p>That’s all folks. You can review <a href="https://github.com/canonall/NewsMediumRecyclerViewExample">the complete project here</a>.</p><p>Any feedback or suggestions are highly appreciated!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=788d353a67bf" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/android-kotlin-recyclerview-with-databinding-788d353a67bf">Android Kotlin RecyclerView with DataBinding</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>