<?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 Bindu Verma on Medium]]></title>
        <description><![CDATA[Stories by Bindu Verma on Medium]]></description>
        <link>https://medium.com/@binduverma_58409?source=rss-4b4452aecaf6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*WlZdQ9tiyKa4yN4p93skpA.jpeg</url>
            <title>Stories by Bindu Verma on Medium</title>
            <link>https://medium.com/@binduverma_58409?source=rss-4b4452aecaf6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 02:31:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@binduverma_58409/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[Understanding Recycler View]]></title>
            <link>https://medium.com/swlh/understanding-recycler-view-831c9c338959?source=rss-4b4452aecaf6------2</link>
            <guid isPermaLink="false">https://medium.com/p/831c9c338959</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[recyclerview]]></category>
            <dc:creator><![CDATA[Bindu Verma]]></dc:creator>
            <pubDate>Mon, 17 Jun 2019 15:19:56 GMT</pubDate>
            <atom:updated>2019-06-25T12:17:02.790Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/549/1*xxb_QJXsnE-CvEn4F64POw.png" /><figcaption>This is how recycler view looks like</figcaption></figure><p><strong>Wi</strong>th the advent of Android Lollipop(i.e.Android 5.0) , the <strong>RecyclerView</strong> made its way officially.In Android, <strong>RecyclerView </strong>is an advanced and flexible version of ListView and GridView. It is a container used for displaying large amount of data sets that can be scrolled very efficiently by maintaining a limited number of views.</p><p>RecyclerView was introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop). Material Design brings lot of new features in Android that changed a lot the visual design patterns regarding the designing of modern Android applications.In RecyclerView android provides a lots of new features which are not present in existing ListView or GridView.</p><p><strong>Why is Recycler View better than ListView?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*9KC98cj7J-q0RWKF1qnIww.png" /></figure><ol><li><strong>Orientation : </strong>ListView can only layout the items in <strong>Vertical </strong>Arrangement and that arrangement cannot be customised according to our requirements. Suppose we need to create a horizontal List then that thing is not feasible with default ListView. But with introduction of RecyclerView we can easily create a <strong>horizontal or Vertical List</strong>. By using <strong>Layout Manager </strong>component of RecyclerView we can easily define the orientation of Items.</li><li><strong>Use Of ViewHolder Pattern : </strong>ListView Adapters do not require the use of ViewHolder but RecyclerView require the use of ViewHolder that is used to store the reference of View’s.</li></ol><p>In ListView it is recommended to use the ViewHolder but it is not compulsion but in RecyclerView it is mandatory to use ViewHolder which is the main difference between RecyclerView and ListView.</p><p>ViewHolder is a static inner class in our Adapter which holds references to the relevant view’s. By using these references our code can <strong>avoid time consuming findViewById()</strong> method to update the widgets with new data.</p><p>3.<strong> Adapters : </strong>In ListView we use many Adapter’s like ArrayAdapter for displaying simple array data, BaseAdapter and SimpleAdapters for custom Lists. In RecyclerView we only use <strong>RecyclerView.Adapter </strong>to set the data in List. The Below code snippet shows how our CustomAdapter looks when we extends RecyclerView.Adapter class and use ViewHolder in it.</p><p>4.<strong>Item Animator:</strong> ListView are lacking in support of good animation. RecyclerView brings a new dimensions in it. By using RecyclerView. <strong>ItemAnimator class</strong> we can easily animate the view.</p><p><strong>5. Item Decoration:</strong> In ListView dynamically decorating items like adding divider or border was not easy but in RecyclerView by using RecyclerView. <strong>ItemDecorator class</strong> we have a huge control on it.</p><p><strong>The most important feature:</strong></p><p>As the name suggests it <strong>recycles </strong>the views which are out of the visibility of the user.For example, if a user scrolled down to a position where the items 4 and 5 are visible; items 1, 2 and 3 would be cleared from the memory to reduce <strong>memory consumption.</strong></p><p><strong>How to add RecyclerView to a project?</strong></p><p>First we have to add the dependency to the build.gradle.</p><pre>dependencies {<br>    implementation &#39;com.android.support:recyclerview-v7:27.0.0&#39;<br>}</pre><p><em>Remember to update the library version to the latest one.You can check it </em><a href="https://developer.android.com/topic/libraries/support-library/packages.html"><em>here</em></a><em>.</em></p><p><strong>Get Started !!</strong></p><p>Step 1:Create a new project and give it a name of your choice</p><p>Step 2:add the dependency stated above in <strong>Gradle Scripts &gt; build.gradle.</strong></p><p>Step 3:Open <strong>res -&gt; layout -&gt; activity_main.xml</strong> (or) <strong>main.xml </strong>and add following code:</p><pre><em>&lt;?xml version=”1.0&quot; encoding=”utf-8&quot;?&gt;</em></pre><pre><em>&lt;android.support.v7.widget.RecyclerView xmlns:android=”http://schemas.android.com/apk/res/android&quot; android:layout_width=”match_parent” android:layout_height=”match_parent” android:id=”@+id/</em><strong><em>programmingList</em></strong><em>”&gt;</em></pre><pre><em> &lt;/android.support.v7.widget.RecyclerView&gt;</em></pre><p>Step 3:Create a new XML <strong>res -&gt; layout -&gt;list_item_layout.xml </strong>add the following code:</p><p>In this step we create a new xml file for item row in which we creates a TextView and ImageView to show the data.</p><pre><em>&lt;?xml version=”1.0&quot; encoding=”utf-8&quot;?&gt;</em></pre><pre><em>&lt;LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&quot; android:layout_width=”match_parent” android:layout_height=”wrap_content” android:orientation=”horizontal” android:padding=”10dp”&gt;</em></pre><pre><em> &lt;ImageView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:src=”@mipmap/ic_launcher” <br>android:id=”@+id/ImgIcon”/&gt; </em></pre><pre><em>&lt;TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” <br>android:text=”@string/app_name” <br>android:layout_weight=”1&quot; <br>android:id=”@+id/txt” <br>android:textSize=”24sp” <br>android:paddingLeft=”16dp” <br>android:layout_gravity=”center”/&gt;<br> &lt;/LinearLayout&gt;</em></pre><p>This XML will look like similar below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-iUJw7qZhuU7ttRuYPaOg.jpeg" /><figcaption><strong>list_item_layout.xml</strong></figcaption></figure><p>Step 4 :</p><pre><em>import android.support.v7.app.AppCompatActivity;<br>import android.os.Bundle;<br>import android.support.v7.widget.LinearLayoutManager;<br>import android.support.v7.widget.RecyclerView; </em></pre><pre><em>public class MainActivity extends AppCompatActivity <br>{ <br>@Override <br>protected void onCreate(Bundle savedInstanceState) <br>{ <br>super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);</em></pre><pre><em> RecyclerView </em><strong><em>programmingList</em></strong><em> = findViewById(R.id.programmingList); programmingList.setLayoutManager(new LinearLayoutManager(this)); </em></pre><pre><em>String[] lang = {“Java”, “Javascript”, “c#”, “python”, “ruby”, “django”, “haskell”, “html”, “go”, “mango”, “apple”}; </em></pre><pre><strong><em>programmingList</em></strong><em>.setAdapter(new ProgrammingAdapter(lang)); <br>}<br>}</em></pre><p>Step 5 : <strong>Create Custom Adapter Class</strong></p><p><strong><em>what is an Adapter class ?</em></strong></p><p>The adapter is the main code responsible for RecyclerView. It holds all the important methods dealing with the implementation of RecyclerView. A RecyclerView adapter just like any other adapter has the responsibility to bind datasets to Views that will be displayed in the window.The basic methods for a successful implementation are:</p><ul><li><strong><em>onCreateViewHolder(</em></strong>ViewGroup parent, int viewType)</li><li><strong>onBindViewHolder</strong>(RecyclerView.ViewHolder holder, int position)</li><li><strong>getItemCount()</strong></li></ul><h4><strong><em>onCreateViewHolder(</em></strong>ViewGroup parent, int viewType)</h4><p>It deals with the inflation of the layout as an item for the RecyclerView.This method is called right when the adapter is created and is used to initialize your ViewHolder(s).</p><p><strong><em>wait what are View Holders ?</em></strong></p><p><em>The views in the list are represented by view holder objects. These objects are instances of a class you define by extending </em><a href="https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder.html"><em>RecyclerView.ViewHolder</em></a><em>. Each view holder is in charge of displaying a single item with a view. For example, if your list shows music collection, each view holder might represent a single album.</em></p><pre>@NonNull<br>@Override<br>public ProgrammingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) <br>{<br> LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view = inflater.inflate(R.layout.list_item_layout, parent, false); <br>return new ProgrammingViewHolder(view); <br>}</pre><h4><strong>onBindViewHolder(RecyclerView.ViewHolder holder, int position)</strong></h4><p>It deals with the setting of different data and methods related to clicks on particular items of the RecyclerView.</p><pre>@Override public void onBindViewHolder(@NonNull ProgrammingViewHolder viewHolder, int i) <br>{<br> String title = data[i]; <br>viewHolder.txt.setText(title);<br> }</pre><h4><strong>getItemCount()</strong></h4><p>It Returns the length of the RecyclerView <em>i.e.the number of items inflated or present in it.</em></p><pre>@Override<br>public int getItemCount()<br> {<br> return data.length;<br> }</pre><p>Now, Let’s create a <em>ViewHolder</em> class (a subclass inside the Adapter class and I’ve named it as ProgrammingViewHolder) and extend <em>RecyclerView.ViewHolder</em></p><pre>public class ProgrammingViewHolder extends ViewHolder<br>{</pre><pre>ImageView m;<br>TextView txt;</pre><pre>public ProgrammingViewHolder(@NonNull View itemView) { </pre><pre>super(itemView);<br>m = itemView.findViewById(R.id.ImgIcon);<br>txt = itemView.findViewById(R.id.txt); <br>}<br>}</pre><p>Now the entire Custom Adapter <strong>(app -&gt; java -&gt; package -&gt; ProgrammingAdapter.java) </strong>looks like:</p><pre>import android.support.annotation.NonNull;<br>import android.support.v7.widget.RecyclerView;<br>import android.support.v7.widget.RecyclerView.ViewHolder;<br>import android.view.LayoutInflater;<br>import android.view.View;<br>import android.view.ViewGroup;<br>import android.widget.ImageView;<br>import android.widget.TextView; </pre><pre>public class ProgrammingAdapter extends RecyclerView.Adapter&lt;ProgrammingAdapter.ProgrammingViewHolder&gt;{ private String[] data;<br> public ProgrammingAdapter(String[] data) <br>{<br> this.data = data; <br>}<br> @NonNull <br>@Override <br>public ProgrammingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) <br>{<br> LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view = inflater.inflate(R.layout.list_item_layout, parent, false);<br> return new ProgrammingViewHolder(view);<br> }<br> @Override<br> public void onBindViewHolder(@NonNull ProgrammingViewHolder viewHolder, int i)<br> {<br> String title = data[i];<br> viewHolder.txt.setText(title);<br> }<br> @Override <br>public int getItemCount() <br>{<br> return data.length;<br> }<br>public class ProgrammingViewHolder extends ViewHolder<br>{<br> ImageView m;<br> TextView txt;<br> public ProgrammingViewHolder(@NonNull View itemView) { super(itemView); <br>m = itemView.findViewById(R.id.ImgIcon);<br> txt = itemView.findViewById(R.id.txt); }<br>}<br>}</pre><p>Now Let’s run our Project.!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IzuA0maez4hBFDAnPNXPqg.png" /><figcaption>This the final look you get</figcaption></figure><p>The entire Source Code can be found <a href="https://github.com/BinduVerma123/Recyler-View">here</a>.😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=831c9c338959" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/understanding-recycler-view-831c9c338959">Understanding Recycler View</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>