DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Introduction to Android App Development With Kotlin: RecyclerView Widget (Part 11)

Introduction to Android App Development With Kotlin: RecyclerView Widget (Part 11)

Learn more about implementing lists in Kotlin with RecyclerView.

Łukasz Mądrzak user avatar by
Łukasz Mądrzak
CORE ·
May. 13, 19 · Tutorial
Like (2)
Save
Tweet
Share
4.02K Views

Join the DZone community and get the full member experience.

Join For Free

Most of the apps you have on your smartphone have a list of things displayed on the screen. Instagram consists of an infinite list of pictures that our friends post. Facebook is a never-ending list of updates from our family and “friends.” Lists are everywhere.

Android allows us to implement such lists using a widget named RecyclerView, and that’s what this tutorial is all about. In the previous part, we have fetched the list of movies from the database, and now, we will display each one of the items in a nice list. Our users will be able to scroll up and down the list as many times as they like to review their list of movies.

Brief Lesson of Android History

Let’s start with a brief lesson of ancient Android history. First, there was a ListView, which is  RecyclerView’s predecessor. Unfortunately, this archaic widget wasn’t very good at memory management, and before it could display anything to the user, it would have to draw the entire list first. This is not ideal when you have hundreds of items in your list.

Then, the RecyclerView was invented and everything changed. You could have hundreds of items in your list but RecyclerView wasn’t phased. The reason for that is that RecyclerView “recycles” the list items. Get it? RecyclerView only ever draws the items that fit on the user's screen, and as the user scrolls, the new items are dynamically drawn. It’s all just a big illusion… Nonetheless, RecyclerView provides a better UX, and now, it’s time for us to implement it.

RecyclerView Widget

RecyclerView consists of a few components that work together to display the data. The overall container for your user interface is a RecyclerView object that you add to your layout. Then, we must provide it a layout manager. Layout manager can be, for example, LinearLayoutManager (items displayed in the list one under the other) or GridLayoutManager (think of the Photos app).

Each item in the list is its own view represented by view holder objects. These objects will extend RecyclerView.ViewHolder.

RecyclerView wouldn’t work without an adapter which is responsible for what gets displayed in the list. This class extends RecyclerView.Adapter<ViewHolder>, and we will implement it in the next tutorial.

Step 1. Imports

Add the following to the dependencies in build.gradle (app) file:

// RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'


Step 2. Add RecyclerView to the view

In the fragment_movie_list.xml file, add the  RecyclerView:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tvTitle"
        android:layout_above="@id/btnAdd"
/>


We want the list to appear between the big title on top of the file and above the button below hence we had to specify id’s for both of those elements.

This is what your preview should look like when you’re done

Image title

Step 3. Set LayoutManager

In the MovieListFragment class in the onViewCreated method, we’ll access this newly created widget using its id and set the LinearLayoutManager on it:

rvList.layoutManager = LinearLayoutManager(activity)


Run the app and ensure no crash occurs. The next step is to create an adapter that will handle the data displayed in the RecyclerView. We’ll get that done in the next tutorial in which we’ll have the data finally displayed on the screen.

Conclusion

Now that you know why we use RecyclerViews and not ListViews, understand the basic mechanism of RecyclerView, and have the basic foundation laid, we must implement an adapter that will tie it all together.

The complete working code can be found in this GitHub repo where each lesson is tagged appropriately.

app Android (robot) Kotlin (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker
  • Fargate vs. Lambda: The Battle of the Future
  • Reliability Is Slowing You Down

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: