Professional Documents
Culture Documents
ListView &
RecyclerView
Introduction
- Its a view capable of displaying scrollable list of
items
- It only creates views (widget) if needed
- It recycles views, e.g. if a row is not displayed
anymore it will be recycled and only its content
will change.
The FLOW diagram
Implement
- Add a ListView object to the layout file of an Activity
- Create a new XML file for the layout of the row in the ListView
- Create your data source — as a list/ array
- Using adapters:
- ArrayAdapter (basic with single text line for every row)
- Custom Adapter (extends BaseAdapter) (complicated layout in every
row)
- Add code to create an instance of the Adapter and populate with your data
source.
Using ArrayAdapter
Whenever you have a list of single items which is backed by an array, you can use
ArrayAdapter. For instance, list of phone contacts, countries or names. Below is
Array Adapter code:
RecyclerView has three main parts: the layout, ViewHolder, and adapter.
The layout is the view that will be created for each item to be loaded into
the RecyclerView.
A ViewHolder is used to cache the view objects in order to save memory.
The adapter creates new items in the form of ViewHolders, populates the
ViewHolders with data, and returns information about the data.
Exercise 2b – Using
RecyclerView
Display a list of countries by using
RecyclerView