You are on page 1of 15

Lesson 4.

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:

val adapter = new


ArrayAdapter<String>(this,R.layout.ListView,R.id.textView,StringArray);
Exercise 1
Display a list of countries by using simple
array adapter
Create a “Custom Adapter”
- Extends "BaseAdapter“
- Extends "ArrayAdapter" can handle data based on Arrays
- Extends "SimpleCursorAdapter" handle database related data
Create a “Custom Adapter”
BaseAdapter is a common base class of a general implementation of an Adapter
that can be used in ListView. Whenever you need a customized list you create
your own adapter and extend base adapter in that. Base Adapter can be
extended to create a custom Adapter for displaying a custom list item.
ArrayAdapter is also an implementation of BaseAdapter.
Exercise 2a – ListView
with Custom Adapter
Display a list of countries by using custom
adapter
Optimized Way
- Using existing rows saves memory and CPU consumption
- convertView - The old view to reuse
- Avoid doing findViewById(), layoutInflater.inflate if not necessary
- ViewHolder pattern
ViewHolder
- The ViewHolder stores a reference to the required views in a row
- ViewHolder is then attached to the row via the method setTag()
- Row is recycled we can get the ViewHolder via getTag() method
onListItemClick
- Parameters
- l: The ListView where the click happened
- v: The view that was clicked within the ListView
- position: The position of the view in the list
- id: The row id of the item that was clicked
RecyclerView
⚫ In Android, RecyclerView is an
advanced and flexible version of
ListView and GridView. RecyclerView
is used to load views to the activity
when you don’t know how many views
we need, or have a large number of
individual item views to display in one
activity. It saves memory by reusing
views when you scroll in an activity
instead of creating them all at the
beginning when the activity first loads.
RecyclerView

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

You might also like