<?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 Isa Rufullazada on Medium]]></title>
        <description><![CDATA[Stories by Isa Rufullazada on Medium]]></description>
        <link>https://medium.com/@isa.rufullazada?source=rss-408cfae82158------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*isDO1WIaKS5hZOyj</url>
            <title>Stories by Isa Rufullazada on Medium</title>
            <link>https://medium.com/@isa.rufullazada?source=rss-408cfae82158------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 09:22:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@isa.rufullazada/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[Shared Element Transition with RecyclerView, Android Kotlin]]></title>
            <link>https://medium.com/@isa.rufullazada/shared-element-transition-with-recyclerview-android-kotlin-5280b36813eb?source=rss-408cfae82158------2</link>
            <guid isPermaLink="false">https://medium.com/p/5280b36813eb</guid>
            <category><![CDATA[xml]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[shared-element-transition]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Isa Rufullazada]]></dc:creator>
            <pubDate>Mon, 28 Nov 2022 09:49:58 GMT</pubDate>
            <atom:updated>2022-11-28T16:17:32.281Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8DN4tIDIBQY4sE3N.png" /></figure><p><strong>RecyclerView </strong>is a widely used View format in Android. If you have heard of <strong>Shared Element Transition</strong>, you might wonder how to implement it to a <strong>RecyclerView </strong>in your project. In this tutorial, I will show you an example of this on a Rick and Morty app.</p><h3><strong>What is Shared Element Transition</strong></h3><p><strong>Shared Element Transition</strong> is a fancy way to add animation to the view that is present in two screens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*e7psYmrG-ou5hv6xd8veBA.gif" /></figure><p>In this project we have two screens:</p><p><strong>CharacterFragment.kt</strong> with <strong>fragment_character.xml</strong></p><p><strong>CharacterDetailsFragment.kt</strong> with <strong>fragment_details_character.xml</strong></p><p>The clicked item from <strong>CharacterFragment.kt </strong>will take us to detailed information in the <strong>CharacterDetailsFragment.kt.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*qQZ3G_8uvZxjOGF28jy5xQ.png" /></figure><h3><strong>What is different using Shared Element Transition with RecyclerView?</strong></h3><p>In order to use <strong>Shared Element Transition</strong>, the view must have the identical <strong>TransitionName</strong> in both Activities/Fragments, which means usually you have to put down view id’s manually. It creates a problem with <strong>RecyclerViews</strong>, because there might be lots of data, and assigning unique id’s to each is not optimal.</p><h3>Before we start</h3><p>Before we start, make sure you use setup <strong>Navigation Component</strong> dependencies and safeargs, because the project is built on Multiple <strong>Fragments</strong>. You can learn more info from them using the links below:</p><ul><li><a href="https://developer.android.com/guide/navigation/navigation-getting-started">https://developer.android.com/guide/navigation/navigation-getting-started</a></li><li><a href="https://developer.android.com/guide/navigation/navigation-pass-data">https://developer.android.com/guide/navigation/navigation-pass-data</a></li></ul><p>Another dependency I used is <strong>Coil</strong>, which helps to load photos from APIs. You can use other libraries such as <strong>Glide</strong> or <strong>Picasso</strong>. Here is the link for the official website of <strong>Coil</strong>:</p><ul><li><a href="https://coil-kt.github.io/coil/">https://coil-kt.github.io/coil/</a></li></ul><h3>RecyclerView design</h3><p>Since the each item in the <strong>RecyclerView</strong> will be used multiple times, <strong>transitionName</strong> attribute cannot be assigned to the item layout. For now, we will leave it be. Here is the <strong>XML</strong> code of each character item.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9274a5f59ca7a47ae4af2b5100f53cd/href">https://medium.com/media/e9274a5f59ca7a47ae4af2b5100f53cd/href</a></iframe><p>And the <strong>RecyclerView XML</strong> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfbc388b9d3cb1cb04a2fa0459bedc4b/href">https://medium.com/media/bfbc388b9d3cb1cb04a2fa0459bedc4b/href</a></iframe><h3>Adapter</h3><p>Now here is the neat part, we can assign transitionNames in the <strong>onBindViewHolder </strong>and each item can have its unique transition id’s according to the data you feed it. You must be sure to put down a name that is unique to that item. In my case, I used characterImage, because each character in the API had a unique image name. You can preferably use id’s for that as well.</p><p>The adapter I used here is <strong>PagingAdapter</strong> for the pagination purpose, but it is identical to the <strong>ListAdapter</strong>. We simply bind names and images of each character. At the end, you can see transitionName<strong> </strong>to be assigned to a value. Keep that in mind, because we will be using that later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30e214d9b87fb0da5a6a7eaa46ec1a8c/href">https://medium.com/media/30e214d9b87fb0da5a6a7eaa46ec1a8c/href</a></iframe><p>In order to move from move from <strong>CharacterFragment.kt</strong> to <strong>CharacterDetailFragment.kt</strong>, the items must be clickable and each clickable item must carry its data to the next page. We can create a OnClickListener class to handle this for us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3047f7e17acf108370872f1a1658f09/href">https://medium.com/media/b3047f7e17acf108370872f1a1658f09/href</a></iframe><p>The OnClickListener class will have a lambda function in the adapter constructor that will transfer <strong>CharacterData</strong> and<strong> ImageView</strong> to the next page using <strong><em>safeargs</em></strong>.</p><p>The adapter code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a4d76c13e610abe383347aaa464bbd5/href">https://medium.com/media/1a4d76c13e610abe383347aaa464bbd5/href</a></iframe><h3>Setting up the transition in the first Fragment</h3><p>In the fragment we should set both <strong>Item Clicking</strong> and the <strong>Shared Element Transition</strong>. val directions determines the destination the click should take. The view is set to the transition name in the val extras . It make it so that a certain view (in our case the characterImageView ) will be animated between <strong>Fragments</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee86fb1e116862d7395f4486f55f320a/href">https://medium.com/media/ee86fb1e116862d7395f4486f55f320a/href</a></iframe><p>Once it is done, then we simply set the adapter to the <strong>RecyclerView</strong> with click listener we created before with the following code:</p><p>characterAdapter = CharactersPagingAdapter(characterDataListener)</p><p>adapter = characterAdapter</p><p>Since I have implemented pagination, there is additional stuff you have to handle to set the adapter. But in core, it is similar to the code above.</p><p>Here is the rest of the code for the setting up <strong>RecyclerView.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25c40936815040cfc7c94776d33b0558/href">https://medium.com/media/25c40936815040cfc7c94776d33b0558/href</a></iframe><p>This fragment is almost complete, the only remaining action is to set the Animation itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40dd86e498c3035dca450dfbe793719e/href">https://medium.com/media/40dd86e498c3035dca450dfbe793719e/href</a></iframe><p>This will initiate animation when we go to the next page from the first fragment, but if we pressed back button, it would not show the animation. It has to be done in a way that when the back button is pressed, the first fragment is already loaded. Thus the following code must be in <strong>onViewCreated</strong>.</p><pre>postponeEnterTransition()</pre><p>Lastly, the enter transition to any view must take place while the it is being drawn. In our project, the recyclerView will be drawn.</p><p>With this, everything is done with the first Fragment. Few setups remain in the second Fragment.</p><p><strong>onViewCreated</strong> in the first fragment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdbd50ce0043533566e9a70f8cdf8e85/href">https://medium.com/media/bdbd50ce0043533566e9a70f8cdf8e85/href</a></iframe><h3>Completing the transition in the second Fragment</h3><p>Firstly, we must retrieve the data from the <strong>first Fragment</strong>. For this, we used safeargs which contains characterData and unique transitionName. It must be retrieved using navArgs() .</p><pre>private val args: CharacterDetailsFragmentArgs by navArgs()</pre><p>After that, we assign our new <strong>ImageView</strong> transitionName to the data we retrieved from the args .</p><pre>private fun setTransition(){<br>    binding.characterImageView.transitionName = args.characterData.image<br>}</pre><p>We are almost there, now just set the animation on <strong>onCreateView</strong> and we are done.</p><pre>val animation = TransitionInflater.from(context).inflateTransition(android.R.transition.move)<br>sharedElementEnterTransition = animation<br>postponeEnterTransition(200, TimeUnit.MILLISECONDS)</pre><p>You can delay the time it takes to start the transition by postponeEnterTransition .</p><p>Now our <strong>onCreateView</strong> will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc8d2a4e7f97ec7214066b9f1bd66e6c/href">https://medium.com/media/bc8d2a4e7f97ec7214066b9f1bd66e6c/href</a></iframe><p>That’s it. You can run the app and it should have a nice transition.</p><h3>Final thoughts</h3><p>I hope this guide helped you to setup <strong>Shared Element Transition</strong>. The project is readily available on my Github repository, you can check it out with the following link:</p><p><a href="https://github.com/isaruff/RickAndMortyApp">GitHub - isaruff/RickAndMortyApp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5280b36813eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>