Jon Rumsey

An online markdown blog and knowledge repository.


Project maintained by nojronatron Hosted on GitHub Pages — Theme by mattgraham

Read Class 28 Notes

Android Developers on RecyclerView for Displaying Lists of Data

Create Dynamic Lists with RecyclerView

A bunch of code is required to get RecyclerView elements to operate. Once you're done struggling to get all this wrapper and setup code implemented, they're pretty alright.

Key Classes

Contain Views corresponding to your data.

Is a View in-and-of-itself.

Add RecyclerView to the Activity Layout as any other View element.

View Holder: Represent individual elements within the RecyclerView container element.

A View Holder must be created and the RecyclerView binds its data to the View Holder.

An Adapter is used by the RecyclerView to request View Holders and Bind its data to them.

Layout Manager is an abstract class that can be used to arrange how the data is displayed by the Recycler View.

Note: Custom Layouts can be defined by extending the abstract Layout Manager.

Implement Recycler View

  1. Decide what the list or grid will look like within the Recycler View.
  2. Design each element's look and behavior. Extend ViewHolder to implement these designs.
  3. Define an Adapter that associates your data with the ViewHolder views.

ViewHolder is the wrapper around View that is managed by RecyclerView.

Play The Layout

LinearLayoutManager: Single-dimensional list.

GridLayoutManager: Two-dimensional grid that can be horizontally or vertically arranged.

StaggeredGridLayoutManager: Similar to GridLayoutManager but allows staggered row and column data layout.

These settings must be made in order to desing the ViewHolder.

Implement Adapter and View Holder

ViewHolder is wrapper around View which contains layout of an individual item in the list.

Adapter creates ViewHolder objects as needed, setting the data for the Views.

Requirements to defining the Adapter:

  1. Override onCreateViewHolder(): Creates and inits ViewHolder and View (no data binding here).
  2. Override onBindViewHolder(): Fetch and associate ViewHolder with fetched data.
  3. Override getItemCount(): RecyclerView calls this to track size of dataset for determining "bottom" of the list.

Also:

Return to Root Readme