Advanced RecyclerView Flashcards

1
Q

Where is the best place to listen for / handle recyclerView item clicks?

A

Listen - ViewHolder (represents just one list item)

Handle - ViewModel (has the data and logic available)

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

List the steps for implementing a ViewHolder ClickListener

A
Create a Listener class
Add a ^variable^ representing the listener to the ^data^ in the item's layout xml
Add onClick property to item xml ConstraintLayout, referencing a data binding lambda (eg "@{( ) -^ clickListener.onClick(sleep)}"
Pass the Listener down through the Adapter constructor to set binding.clickListener = clickListener in ViewHolder.bind()
Add functionality handling the click by instantiating a Listener in the Adapter's constructor in Fragment.onCreateView()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the Codelabs pattern for handling RecyclerView Clicks?

A
  • clickListener instantiated in Adapter’s constructor in Fragment.onCreateView( ) passes item id to a viewModel function
  • viewModel function uses item id to set a MutableLiveData.value
  • functionality handling the click is set in the MutableLiveData’s observer function in Fragment.onCreateView( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you define a Listener for ViewHolder clicks?

A
class MyItemListener(val clickListener: (itemId:Long) -^ Unit){
fun onClick(entity: Entity) = clickListener(entity.entityId)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List the steps for adding a header item to the RecyclerView

A
  • Create a sealed class containing subclasses representing the different types of item and an object representing the header item
  • Add a layout xml for the header item
  • Create a new ViewHolder class for the Header item
  • Update the Adapter (see another card)
  • Update the DiffCallback class (replace item type with sealed class type)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List the steps for updating the adapter when adding a header item to the RecyclerView

A
a) assign constant values for different view types eg
private val ITEM_VIEW_TYPE_HEADER = 0
b) override getItemViewType - use a return when( ) to return the correct int constant depending on the item returned by getItem(position)
c) replace references to item type in class / function definitions with reference to parent sealed class
d) replace references to ViewHolder type with RecyclerView.ViewHolder
e) use a when in all Adapter functions to perform different logic depending on the the item type
f) create a new function to wrap Adapter.submitList( ) so it always includes a header.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define the function for wrapping Adapter.submitList so it always returns the header but only adds items of a list if the list is not null.

A
fun addHeaderAndSubmitList(list: List?) {
  val items = when (list) {
    null -> listOf(DataItem.Header)
    else -> listOf(DataItem.Header) + list.map { 
      DataItem.SleepNightItem(it) }
    }
  submitList(items)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When working with large lists, how do you ensure that Adapter work does not block the main thread?

A

Wrap work preparatory to submitList( ) (mapping the list to RecyclerView items, adding the header) in a CoroutineScope before calling submitList( ) on the main thread

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

What is needed to display one item across several spans?

A

manager.spansizeLookup = object : GridLayoutManager.SpanSizeLookup( ){
override fun getSpanSize(position: Int) = when (position) {
(position of item requiring X spans) -> X
else -> 1
}
}

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