RecyclerView Basics Flashcards
What is the role of a RecyclerView Adapter?
- Adapts a list so the RecyclerView can display its items
2. Produces ViewHolders
What does a ViewHolder contain?
- Views
- Data
- Meta information
How does the RecyclerView know how many items to display?
getItemCount() in the Adapter
Which two Adapter methods bind ViewHolders to data?
onCreateViewHolder()
and
onBindViewHolder()
What 6 parts are required for a RecyclerView?
Data to be displayed RecyclerView Instance in layout xml Item layout xml Layout Manager ViewHolder Adapter
How are Views presented in the provided LinearLayoutManager?
A vertical list of full width rows
Which 3 LayoutManagers are suggested by Android Studio?
LinearLayoutManager
GridLayoutManager
StaggeredGridLayoutManager
What is the core task in implementing a RecyclerView?
Creating the Adapter
Which 3 functions must be implemented in the Adapter?
onCreateViewHolder()
onBindViewHolder()
getItemCount()
What will the functions in the Adapter be called by?
The RecyclerView
What parameters are taken by onCreateViewHolder() ?
parent: ViewGroup (the RecyclerView)
viewType: int
What is the role of the LayoutInflater?
It creates a View from a layout xml
What is the syntax for creating a LayoutInflater?
val layoutInflater = LayoutInflater.from(parentView.context)
Why is attachToRoot false when inflating a View?
Attaching the child View to its parent is not the responsibility of onCreateViewHolder()
Which 3 parameters are passed to LayoutInflater.inflate() ?
- The layout xml to be inflated into a View
- The parent View to which this (return, child) View will be attached
- attachToRoot: Boolean
What is the role of onCreateViewHolder() ?
Converts layout xml to a View in a ViewHolder
What is the role of onBindViewHolder() ?
Binds data from the list to the View in the ViewHolder
Which function can the RecyclerView call to renew all its data?
notifyDataSetChanged()
What parameters are taken in the constructor of an Adapter?
None - we don’t declare a constructor for an Adapter class
How is an Adapter attached to a RecyclerView?
In Fragment.onCreateView()
using
val adapter = MyAdapter()
binding.recyclerViewXmlId.adapter = adapter
Where is the data in Adapter instantiated / updated?
Fragment.onCreateView() using binding.list.observe(viewLifecycleOwner) { it?.let{ adapter.data = it }
What parameters are taken by onBindViewHolder() ?
- holder: YourViewHolder
2. position: Int
Why would a RecyclerView need to access ViewHolder.itemView? (3)
- Binding an item to display on the screen
- Drawing decorations around a view like a border
- Implementing accessibility
What does ViewHolder.itemView return?
The ViewHolder’s View (the one that was inflated in onCreateViewHolder)
What is the usual class signature for a ViewHolder?
class MyViewHolder private constructor(itemView: View) : RecyclerView.ViewHolder(itemView)
What is the class signature for a ViewHolder that is simply a wrapper for a TextView?
class MyViewHolder private constructor(textView: TextView) : RecyclerView.ViewHolder(textView)
What is the class signature for an Adapter that uses a ViewHolder which is defined within the Adapter class itself?
class MyAdapter : RecyclerView.Adapter^MyAdapter.MyViewHolder^()