<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jimmy Mayoukou</title>
    <description>The latest articles on DEV Community by Jimmy Mayoukou (@bhullnatik).</description>
    <link>https://dev.to/bhullnatik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F291058%2Fce34aac4-063d-4da7-8d15-dd9fbd2fac5a.jpeg</url>
      <title>DEV Community: Jimmy Mayoukou</title>
      <link>https://dev.to/bhullnatik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhullnatik"/>
    <language>en</language>
    <item>
      <title>How to use Material dialogs with DialogFragment</title>
      <dc:creator>Jimmy Mayoukou</dc:creator>
      <pubDate>Sat, 23 May 2020 08:59:16 +0000</pubDate>
      <link>https://dev.to/bhullnatik/how-to-use-material-dialogs-with-dialogfragment-28i1</link>
      <guid>https://dev.to/bhullnatik/how-to-use-material-dialogs-with-dialogfragment-28i1</guid>
      <description>&lt;p&gt;In this article, I'll tell you how to use the new Material dialogs with &lt;a href="https://developer.android.com/reference/androidx/fragment/app/DialogFragment" rel="noopener noreferrer"&gt;&lt;code&gt;DialogFragment&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Material dialogs are a re-styling of commonly-used &lt;a href="https://developer.android.com/reference/android/app/Dialog.html" rel="noopener noreferrer"&gt;&lt;code&gt;Dialog&lt;/code&gt;&lt;/a&gt;. I won't go into too many details on what they do or how to customize them here, you can read this great article from Nick Rout: &lt;a href="https://medium.com/over-engineering/hands-on-with-material-components-for-android-dialogs-75c6d726f83a" rel="noopener noreferrer"&gt;Hands-on with Material Components for Android: Dialogs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here is, for comparison, an AppCompat &lt;code&gt;AlertDialog&lt;/code&gt; and a Material &lt;code&gt;AlertDialog&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1ci1lbgjjdvim992k39z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1ci1lbgjjdvim992k39z.png" alt="AppCompat Alert Dialog vs Material Dialog"&gt;&lt;/a&gt;&lt;br&gt;
You can see the differences in default shadow, size, typography, etc... But the real advantage is to be able to support Material typography and shape out of the box with theme configuration.&lt;/p&gt;

&lt;p&gt;For example, here is the result with bold title and custom shape (You can look into &lt;a href="https://github.com/Bhullnatik/MaterialDialogsFragmentExample/blob/master/app/src/main/res/values/styles.xml" rel="noopener noreferrer"&gt;styles.xml&lt;/a&gt; for how to do this):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmjmammu0dl4szm8lj6k3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmjmammu0dl4szm8lj6k3.png" alt="Customized Material AlertDialog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice isn't it? Except it is quite limited to use only &lt;code&gt;Dialog&lt;/code&gt;. You can't use complex layouts other than the 4 types mentioned in Nick's article, or use them with more complex view hierarchy, or from &lt;a href="https://developer.android.com/guide/navigation/navigation-getting-started#create-dialog" rel="noopener noreferrer"&gt;Navigation Architecture Component&lt;/a&gt;. For all that, you will have to use a &lt;a href="https://developer.android.com/reference/androidx/fragment/app/DialogFragment" rel="noopener noreferrer"&gt;&lt;code&gt;DialogFragment&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you try to display a &lt;code&gt;DialogFragment&lt;/code&gt; now, we will see it still looks like a regular &lt;code&gt;AlertDialog&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7lgrlw14l8eeg10xlsr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7lgrlw14l8eeg10xlsr7.png" alt="AppCompat DialogFragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's move onto the solution on how to use Material dialog with &lt;code&gt;DialogFragment&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DialogFragment&lt;/code&gt;s are useful to show more complex layouts in a dialog way, allowing you to control and manipulate your custom layout. They act like regular &lt;a href="https://developer.android.com/guide/components/fragments" rel="noopener noreferrer"&gt;&lt;code&gt;Fragment&lt;/code&gt;&lt;/a&gt;, meaning you have to override &lt;a href="https://developer.android.com/reference/androidx/fragment/app/Fragment#onCreateView(android.view.LayoutInflater,%20android.view.ViewGroup,%20android.os.Bundle)" rel="noopener noreferrer"&gt;&lt;code&gt;onCreateView()&lt;/code&gt;&lt;/a&gt; &amp;amp; &lt;a href="https://developer.android.com/reference/androidx/fragment/app/Fragment#onViewCreated(android.view.View,%20android.os.Bundle)" rel="noopener noreferrer"&gt;&lt;code&gt;onViewCreated()&lt;/code&gt;&lt;/a&gt; to display your custom view.&lt;/p&gt;

&lt;p&gt;To use Material dialogs with &lt;code&gt;DialogFragment&lt;/code&gt;, you can override &lt;a href="https://developer.android.com/reference/kotlin/androidx/fragment/app/DialogFragment?hl=en#oncreatedialog" rel="noopener noreferrer"&gt;&lt;code&gt;onCreateDialog()&lt;/code&gt;&lt;/a&gt; in your &lt;code&gt;DialogFragment&lt;/code&gt;, to return a &lt;code&gt;MaterialDialog&lt;/code&gt;. It is mentionned briefly by Nick in their article, you can see the example on &lt;a href="https://github.com/Bhullnatik/MaterialDialogsFragmentExample/commit/addb9c2db8b9b57fe04748b0010165a427583204#diff-3de1fb467da1da746330c0ad22bf49b9R23" rel="noopener noreferrer"&gt;this commit&lt;/a&gt;. So implement it, show it and you get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft14ybfl3xkjuqjpu9atb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft14ybfl3xkjuqjpu9atb.png" alt="MaterialDialogBuilder with DialogFragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;...Nothing. How is this possible? &lt;/p&gt;

&lt;p&gt;It's because &lt;code&gt;DialogFragment&lt;/code&gt; ignores &lt;code&gt;onCreateView()&lt;/code&gt; if you override &lt;code&gt;onCreateDialog()&lt;/code&gt;, as it assumes the dialog will take care of its own view. As the docs say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;when doing so, &lt;code&gt;onCreateView(LayoutInflater, ViewGroup, Bundle)&lt;/code&gt; does not need to be implemented since the AlertDialog takes care of its own content.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The solution would be to use &lt;a href="https://developer.android.com/reference/android/app/Dialog#setContentView(android.view.View)" rel="noopener noreferrer"&gt;&lt;code&gt;setView()&lt;/code&gt;&lt;/a&gt; on our &lt;code&gt;MaterialDialogBuilder&lt;/code&gt; inside &lt;code&gt;onCreateDialog&lt;/code&gt;. You can check the example at &lt;a href="https://github.com/Bhullnatik/MaterialDialogsFragmentExample/blob/5b9890ebe8c96ac8738cba223269b7cee5050b03/app/src/main/java/io/bhullnatik/materialdialogsfragmentexample/ExampleDialogFragment.kt#L13" rel="noopener noreferrer"&gt;this commit&lt;/a&gt; and it works! 🎉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Funw5gdiruzsiir1boini.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Funw5gdiruzsiir1boini.png" alt="Customized DialogFragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem with this approach, is that for complex layouts it is difficult to manage as we would have to handle all communications with our views when creating the &lt;code&gt;Dialog&lt;/code&gt;, which is not very clean. &lt;/p&gt;

&lt;p&gt;It can also cause sneaky crashes, as &lt;a href="https://developer.android.com/reference/androidx/fragment/app/Fragment#getView()" rel="noopener noreferrer"&gt;&lt;code&gt;getView()&lt;/code&gt;&lt;/a&gt; on our &lt;code&gt;DialogFragment&lt;/code&gt; will return &lt;code&gt;null&lt;/code&gt;, since the view wasn't properly initialized this way. It also breaks &lt;a href="https://developer.android.com/topic/libraries/view-binding" rel="noopener noreferrer"&gt;&lt;code&gt;ViewBinding&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://antonioleiva.com/kotlin-android-extensions/" rel="noopener noreferrer"&gt;Kotlin synthetics&lt;/a&gt;, which may require painful migrations on larger codebases. &lt;/p&gt;

&lt;p&gt;So is there a better way?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The hack:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It turns out we have pretty much everything we need at hand to make it work without needing to migrate existing code.&lt;/p&gt;

&lt;p&gt;In a new class inheriting &lt;code&gt;DialogFragment&lt;/code&gt;, we can override &lt;code&gt;onCreateDialog()&lt;/code&gt; to make it use &lt;code&gt;onCreateView()&lt;/code&gt; &amp;amp; &lt;code&gt;onViewCreated()&lt;/code&gt; to display and  manage the &lt;code&gt;Fragment&lt;/code&gt;'s layout, like so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCreateDialog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="p"&gt;?):&lt;/span&gt; &lt;span class="nc"&gt;Dialog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;MaterialAlertDialogBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;requireContext&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;dialogView&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;onCreateView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LayoutInflater&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;requireContext&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="nf"&gt;setView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dialogView&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that we don't need to pass a proper &lt;code&gt;container&lt;/code&gt; here to &lt;code&gt;onCreateView&lt;/code&gt; since this is used when the fragment will be added to a view hierarchy, which is rarely the case for a &lt;code&gt;DialogFragment&lt;/code&gt;. We also properly restoring the state with &lt;code&gt;savedInstanceState&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can then override the &lt;code&gt;getView()&lt;/code&gt;  for our &lt;code&gt;Fragment&lt;/code&gt; as to fix accessing our views:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getView&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dialogView&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This basically redirects view accessors to refer to our dialog's custom view, fixing in the process tools making it easier to access views inside &lt;code&gt;Fragment&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;We are guaranteed that it will be valid for the lifecycle of the &lt;code&gt;Dialog&lt;/code&gt;, and therefore of the &lt;code&gt;DialogFragment&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;And that's it! You can put that into a new &lt;code&gt;MaterialDialogFragment&lt;/code&gt; class, change your &lt;code&gt;DialogFragment&lt;/code&gt; inheritance to &lt;code&gt;MaterialDialogFragment&lt;/code&gt;, and enjoy your hassle-free Material dialogs. You can find the complete &lt;a href="https://github.com/Bhullnatik/MaterialDialogsFragmentExample" rel="noopener noreferrer"&gt;complete example here&lt;/a&gt; and the &lt;a href="https://github.com/Bhullnatik/MaterialDialogsFragmentExample/blob/master/app/src/main/java/io/bhullnatik/materialdialogsfragmentexample/MaterialDialogFragment.kt" rel="noopener noreferrer"&gt;&lt;code&gt;MaterialDialogFragment&lt;/code&gt; class here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope it could help you in some way, don't hesitate to comment if you have a question or suggestion. 👋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚠️ Disclaimer:&lt;/strong&gt; This was not battle-tested and could break/not handle everything a &lt;code&gt;DialogFragment&lt;/code&gt; does in some situations. Use at your own risk. There is an issue open on this subject, that you can follow here: &lt;a href="https://github.com/material-components/material-components-android/issues/540" rel="noopener noreferrer"&gt;MaterialComponents Android: Use MaterialDialog with Navigation Component - Issue #540&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>dialog</category>
      <category>materialdesign</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>How to access views directly with ViewPager2</title>
      <dc:creator>Jimmy Mayoukou</dc:creator>
      <pubDate>Fri, 13 Dec 2019 07:05:00 +0000</pubDate>
      <link>https://dev.to/bhullnatik/how-to-access-views-directly-with-viewpager2-3bo8</link>
      <guid>https://dev.to/bhullnatik/how-to-access-views-directly-with-viewpager2-3bo8</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR:
&lt;/h2&gt;

&lt;p&gt;You can use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewPager2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getChildAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to access the internal &lt;code&gt;RecyclerView&lt;/code&gt; used inside the &lt;code&gt;ViewPager2&lt;/code&gt; for now, and you can call &lt;a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)"&gt;&lt;code&gt;findViewHolderForAdapterPosition()&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.html#findViewHolderForItemId(long)"&gt;&lt;code&gt;findViewHolderForItemId()&lt;/code&gt;&lt;/a&gt; as usual.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation:
&lt;/h2&gt;

&lt;p&gt;I had a custom view implementing a &lt;code&gt;ViewPager&lt;/code&gt; which could, in some cases, modify the inner views without having to recompute all of them for a small change.&lt;/p&gt;

&lt;p&gt;This is not possible to do as such anymore, as the internal implementation is a bit different. As you know, &lt;code&gt;ViewPager2&lt;/code&gt; now uses a &lt;a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter.html"&gt;&lt;code&gt;RecyclerView.Adapter&lt;/code&gt;&lt;/a&gt; to handle the paged views, but how is it used internally?&lt;/p&gt;

&lt;h4&gt;
  
  
  Internals:
&lt;/h4&gt;

&lt;p&gt;If we look at the children of an initialized &lt;code&gt;ViewPager2&lt;/code&gt;, we can see there is only one, a &lt;code&gt;RecyclerViewImpl&lt;/code&gt;. &lt;a href="https://github.com/aosp-mirror/platform_frameworks_support/blob/androidx-master-dev/viewpager2/src/main/java/androidx/viewpager2/widget/ViewPager2.java#L927"&gt;&lt;code&gt;RecyclerViewImpl&lt;/code&gt;&lt;/a&gt; is a new internal class used to manage the &lt;code&gt;ViewPager2&lt;/code&gt; internally, bringing a lot of benefits that &lt;a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.html"&gt;&lt;code&gt;ReyclerView&lt;/code&gt;s&lt;/a&gt; have to &lt;code&gt;ViewPager&lt;/code&gt;. Unfortunately, this class is private and therefore inaccessible outside of &lt;code&gt;ViewPager2&lt;/code&gt;. So we're stuck?&lt;/p&gt;

&lt;p&gt;Nope! &lt;code&gt;RecyclerViewImpl&lt;/code&gt; inherits from &lt;code&gt;RecyclerView&lt;/code&gt;, and it should be enough in most cases, and thankfully in ours, to use.&lt;/p&gt;

&lt;h5&gt;
  
  
  Solution:
&lt;/h5&gt;

&lt;p&gt;So then we can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewPager2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getChildAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to access the internal &lt;code&gt;RecyclerView&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can quickly wrap that in a Kotlin extension function to hide the complexity and simplify the use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;ViewPager2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findViewHolderForAdapterPosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ViewHolder&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getChildAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;RecyclerView&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;findViewHolderForAdapterPosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can adapt to something that suits your needs better of course, like for example directly returning the &lt;code&gt;ViewHolder.itemView&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚠️ Warning :&lt;/strong&gt; This relies on internal implementation, which could change at any time without warning. For now this is the only way that I found to access the underlying views, but a feature request is open &lt;a href="https://issuetracker.google.com/issues/146101900"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>viewpager2</category>
      <category>kotlin</category>
      <category>androidx</category>
    </item>
  </channel>
</rss>
