RecyclerView Basics Flashcards

1
Q

What is the role of a RecyclerView Adapter?

A
  1. Adapts a list so the RecyclerView can display its items

2. Produces ViewHolders

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a ViewHolder contain?

A
  1. Views
  2. Data
  3. Meta information
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does the RecyclerView know how many items to display?

A

getItemCount() in the Adapter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which two Adapter methods bind ViewHolders to data?

A

onCreateViewHolder()
and
onBindViewHolder()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What 6 parts are required for a RecyclerView?

A
Data to be displayed
RecyclerView Instance in layout xml
Item layout xml
Layout Manager
ViewHolder
Adapter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are Views presented in the provided LinearLayoutManager?

A

A vertical list of full width rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which 3 LayoutManagers are suggested by Android Studio?

A

LinearLayoutManager
GridLayoutManager
StaggeredGridLayoutManager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the core task in implementing a RecyclerView?

A

Creating the Adapter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which 3 functions must be implemented in the Adapter?

A

onCreateViewHolder()
onBindViewHolder()
getItemCount()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will the functions in the Adapter be called by?

A

The RecyclerView

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What parameters are taken by onCreateViewHolder() ?

A

parent: ViewGroup (the RecyclerView)
viewType: int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the role of the LayoutInflater?

A

It creates a View from a layout xml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax for creating a LayoutInflater?

A

val layoutInflater = LayoutInflater.from(parentView.context)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is attachToRoot false when inflating a View?

A

Attaching the child View to its parent is not the responsibility of onCreateViewHolder()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which 3 parameters are passed to LayoutInflater.inflate() ?

A
  1. The layout xml to be inflated into a View
  2. The parent View to which this (return, child) View will be attached
  3. attachToRoot: Boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the role of onCreateViewHolder() ?

A

Converts layout xml to a View in a ViewHolder

17
Q

What is the role of onBindViewHolder() ?

A

Binds data from the list to the View in the ViewHolder

18
Q

Which function can the RecyclerView call to renew all its data?

A

notifyDataSetChanged()

19
Q

What parameters are taken in the constructor of an Adapter?

A

None - we don’t declare a constructor for an Adapter class

20
Q

How is an Adapter attached to a RecyclerView?

A

In Fragment.onCreateView()
using
val adapter = MyAdapter()
binding.recyclerViewXmlId.adapter = adapter

21
Q

Where is the data in Adapter instantiated / updated?

A
Fragment.onCreateView()
using
binding.list.observe(viewLifecycleOwner) {
  it?.let{
    adapter.data = it
    }
22
Q

What parameters are taken by onBindViewHolder() ?

A
  1. holder: YourViewHolder

2. position: Int

23
Q

Why would a RecyclerView need to access ViewHolder.itemView? (3)

A
  1. Binding an item to display on the screen
  2. Drawing decorations around a view like a border
  3. Implementing accessibility
24
Q

What does ViewHolder.itemView return?

A

The ViewHolder’s View (the one that was inflated in onCreateViewHolder)

25
Q

What is the usual class signature for a ViewHolder?

A

class MyViewHolder private constructor(itemView: View) : RecyclerView.ViewHolder(itemView)

26
Q

What is the class signature for a ViewHolder that is simply a wrapper for a TextView?

A

class MyViewHolder private constructor(textView: TextView) : RecyclerView.ViewHolder(textView)

27
Q

What is the class signature for an Adapter that uses a ViewHolder which is defined within the Adapter class itself?

A

class MyAdapter : RecyclerView.Adapter^MyAdapter.MyViewHolder^()