RecyclerView with Data Binding Flashcards

1
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^()

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

What is Context?

A
An interface to global information about an application environment.
An abstract class whose implementation is provided by the Android system.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can a Context provide access to?

A

App-specific resources and classes
Up-calls for app-level operations, ie
-launching Activities
-broadcasting and receiving Intents

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

In which class should a ViewHolder’s Views be set?

A

Set the Views in a ViewHolder.bind() function, then call holder.bind from Adapter.onBindViewHolder()

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

In which class should the ViewHolder’s View be inflated?

A

In a companion object of the ViewHolder class, so the function responsible for inflating can be called from Adapter.onCreateViewHolder()

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

Why is the ViewHolder’s constructor private?

A

Because the companion object returns a ViewHolder instance, the constructor never needs to be called

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

What is the class signature when extending DiffUtil.ItemCallback?

A

class MyListObjectDiffCallback: DiffUtil.ItemCallback^MyListObject^()

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

What functions are implemented when extending DiffUtil.ItemCallback?

A

areItemsTheSame(oldItem: MyListObject, newItem: MyListObject): Boolean

areContentsTheSame(oldItem: MyListObject, newItem: MyListObject): Boolean

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

What is the role of a ListAdapter?

A

Keeps track of the list and notifies the Adapter when the list is updated

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

What is the class signature for implementing a ListAdapter?

A

class MyAdapter: ListAdapter^MyListObject, MyViewHolder^(MyListObjectDiffCallback( ) )

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

What can be removed from the Adapter when the ListAdapter is implemented?

A

The property keeping track of the list of data

The function getItemCount( )

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

Where does the function getItem(position) (provided by ListAdapter) need to be used?

A

Adapter.onBindViewHolder()
getItem(position) returns the item to be displayed - the function is provided as the list is not available to the Adapter when the responsibility of maintaining the list is taken over by the ListAdapter

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

How do you use ListAdapter.submitList() ?

A
In Fragment.onCreateView()
Call submitList(list) on the adapter property that has been attached to the recyclerView.
This will likely be in the lambda of the observer of the LiveData^List^
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between inflating a ViewHolder View with and without data binding?

A

Without data binding:

  • inflate a view using layoutInflater.inflate()
  • pass the view to the returned MyViewHolder(view)
  • this is passed to the parent RecyclerView.ViewHolder(view)

With data binding:

  • inflate a binding object using GeneratedViewDataBinding.inflate(layoutInflater, parent, false)
  • pass the binding in as the parameter to instantiate the returned MyViewHolder(binding)
  • The parent RecyclerView.ViewHolder requires a view as its parameter, so pass in binding.root
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does the ViewHolder class signature change when implementing data binding?

A
  • (itemView: View) becomes (binding: GeneratedViewDataBinding) (name will match the layout xml to be inflated)
  • Put “val” in front of “binding” in the constructor so binding is initialised when the constructor is called and can be used throughout the ViewHolder class.
  • binding.root replaces itemView being passed to parent RecyclerView.ViewHolder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the syntax for setting inlined views using the binding object in onBindViewHolder()?

A

binding.view.text = “String”

17
Q

What are the steps to implement binding adapters in the bind() function?

A
  • Create BindingUtils.kt file
  • Create @BindingAdapters - extension functions that set the views in the ViewHolder
  • Replace (inline) binding setters with
    a) binding.layoutXmlDataVariableName = item
    b) binding.executePendingBindings( )
  • Add bindings to the xml layout using
    • app:functionIdentifier=”@{layoutXmlDataVariableName}
18
Q

In the bind() function, what is the purpose of

binding.layoutXmlDataVariableName = item

A

It associates the ViewHolder’s entity instance with the binding

19
Q

In the bind function, what is the purpose of

binding.executePendingBindings( )

A

It executes any pending @BindingAdapter functions right away, immediately setting the Views in the ViewHolder

20
Q

What arguments does a GridLayoutManager take?

A
  1. context (pass “activity”)
  2. noOfColumns: Int
  3. GridLayoutManager.VERTICAL //vertical is the default orientation
  4. isReverseLayout: Boolean

(3 and 4 optional)

21
Q

How do you set the RecyclerView’s LayoutManager to GridLayoutManager?

A

In Fragment.onCreateView()
val manager = GridLayoutManager(…)
binding.recyclerViewsLayoutXmlId.layoutManager = manager

22
Q

How do Binding Adapter functions need to change once click listeners and handlers are implemented on the RecyclerView?

A

Items being passed to @BindingAdapter methods might now be nullable. Make the item argument to be of nullable type and wrap functionality in item?.let{ }