View Recycling in Android with ListView
Last Updated :
18 Feb, 2021
Memory management is a crucial aspect of app development. Since mobile devices have very limited memory, it is necessary to use it carefully in our applications. One of the best practices involved in doing so is "
view recycling".
This article is about view recycling in Android and then a simple app is created which implements the practice of view recycling using
ListView and
ArrayAdapter in conjunction.
Need for View Recycling in Android
It is a practice to use as little memory as possible by recycling unused views to display new content instead of creating new views for the same. Suppose, we are scrolling down through a list of one thousand words. If we create a
TextView for each word, we would need one thousand
TextViews for this. This would waste a lot of memory since our device's screen displays only 7-8
TextViews at a time and we need to scroll down if we want to see the rest of them.
When we scroll down, the
TextViews which are at the top are not visible anymore. So, an inference can be taken that the top
TextViews are not used by the user when they scroll down the ListView. Hence, unused
TextViews are recycled and are used at the bottom when the user scrolls down. This way, instead of having one thousand
TextViews, our task can be achieved with a few of them only.
Example for View Recycling in Android
One of the most common example is our mobile's phone-book. We can have many contacts on our phone but instead of creating a new
TextViews for each contact, our phone just recycles the unused scrolled up/down views and fills them with the new contact information and displays it again when the user scrolls up/down.
Implementation of View Recycling using ArrayAdapter and Listview
- ArrayAdapter is a Java public class which extends from BaseAdapter class. An ArrayAdapter object makes the data (which is to be displayed) adapt to an array. Basically adapter is a bridge between UI component and data that helps to fill data in the UI component.
- ListView is a Java public class which extends from AbsListView. ListView is a view which groups several items and displays them in a vertical list. This list is also automatically made scrollable if the amount of data supplied cannot be accommodated on the screen.
- ArrayAdapter and ListView are required for view recycling. ListView asks for views from ArrayAdpapter by sending it a request and a specified position. ArrayAdpapter then returns the view at the specified position as the ListView keeps on asking for it until the device's screen is filled. Now, when the user scrolls down, ListView gives ArrayAdpapter the top views which aren't displayed on the device's screen anymore. The ArrayAdpapter then erases the previous data of that ScrapView and sets new data and returns it to the ListView instead of creating a new view!
Below is a simple app to demonstrate this practice of memory management.
- Step 1: Add the below code in activity_main.xml file which would just contain a ListView and a TextView.
activity_main.xml
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#66bb6a"
android:padding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stuff you can learn at GeeksforGeeks:"
android:background="#a5d6a7"
android:textSize="22sp"
android:fontFamily="sans-serif-condensed-light"
android:textColor="#fafafa"/>
<ListView
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Output:
- Step 2: In the below code, when we initialize the ArrayAdapter, we pass a layout called android.R.layout.simple_list_item_1 along with our ArrayList and Context. The android.R.layout.simple_list_item_1 is a inbuilt layout that describes the design in which a single list item will be shown. It conventionally consists of just a single TextView
Once the ListView and ArrayAdapter are initialized, set the ArrayAdapter on the ListView using the setAdapter() method.
MainActivity.java
package com.example.gfgrecycleview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity
extends AppCompatActivity {
public static final
String LOG_TAG
= MainActivity.class
.getName();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a list of study fields.
ArrayList<String> stuff = new ArrayList<>();
stuff.add("Data Structures");
stuff.add("Algorithms");
stuff.add("Competitive Programming");
stuff.add("Interview Questions");
stuff.add("Python");
stuff.add("Java");
stuff.add("Designing");
stuff.add("Coding");
stuff.add("Developing");
stuff.add("Project Ideas");
stuff.add("C++");
stuff.add("Basically Everything!");
// Find a reference to the
//{@link ListView} in the layout
ListView itemListView
= (ListView)findViewById(R.id.list);
// Create a new {@link ArrayAdapter}
// of study fields
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
stuff);
// Set the adapter
// on the {@link ListView}
// so the list can be populated
/// in the user interface
itemListView.setAdapter(adapter);
}
}
Output:
Similar Reads
SearchView in Android with ListView
SearchView is a widget provided by the Android framework that allows users to search for specific data within an application. It is commonly used in apps that have large amounts of data or content that can be searched, such as contacts, music, or emails. The SearchView widget consists of an input fi
2 min read
RecyclerView using ListView in Android With Example
RecyclerView is a more flexible and advanced version of ListView and GridView. RecyclerView is used for providing a limited window to a large data set, which means it is used to display a large amount of data that can be scrolled very efficiently by maintaining a limited number of Views. In Recycler
5 min read
SearchView in Android with RecyclerView
Many apps represent data in the form of huge lists and for filtering these lists we have seen the SearchView present inside these apps. So for filtering this list of data we generally use a SearchView. In this article, we will take a look at the implementation of Search View in Android with a Recycl
10 min read
SearchView in Android with Kotlin
SearchView is a widget in android which provides a search interface with the help of which users can be able to make searches within the given list of data. In this search view, the user has to specify the search query. According to the search, query results will be populated within the listview. In
4 min read
Android RecyclerView in Kotlin
In this article, you will know how to implement RecyclerView in Android using Kotlin . Before moving further let us know about RecyclerView. A RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ab
4 min read
RecyclerView in Android with Example
RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be custo
7 min read
RecyclerView Multiple View Types in Android with Example
We've all used RecyclerView in Android to display a list in our applications. RecyclerView is used to display emails in the Gmail app, feeds on Facebook and Instagram, and messages in WhatsApp, among other things. However, as being an Android Developer you would have come across a fact that when we
5 min read
Expandable RecyclerView in Android with Kotlin
In this article, we will get to know how to implement the expandable RecyclerView. In General, we will show the list of items in the listview but in some situations like if you want to show the sub-list items, then we have to use an expandable list. See the below image for a better understanding. ex
7 min read
ViewFlipper in Android with Kotlin
View Flipper is a UI component in Android which is used to flip the view within the screen in the android application. The View Flipper class is an extension of ViewAnimator class. With the help of this, we can simply flip the views. In this article, we will take a look at How we can use ViewFlipper
4 min read
Android - Difference Between RecyclerView and ListView
In Android View is a basic building block of UI (User Interface). A view is a small rectangular box that responds to user inputs. RecyclerView and ListView are the two major Views in Android. So in this article, we are going to see the major differences between these two views. RecyclerView Recycler
3 min read