RecyclerView with Data Binding Flashcards
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^()
What is Context?
An interface to global information about an application environment. An abstract class whose implementation is provided by the Android system.
What can a Context provide access to?
App-specific resources and classes
Up-calls for app-level operations, ie
-launching Activities
-broadcasting and receiving Intents
In which class should a ViewHolder’s Views be set?
Set the Views in a ViewHolder.bind() function, then call holder.bind from Adapter.onBindViewHolder()
In which class should the ViewHolder’s View be inflated?
In a companion object of the ViewHolder class, so the function responsible for inflating can be called from Adapter.onCreateViewHolder()
Why is the ViewHolder’s constructor private?
Because the companion object returns a ViewHolder instance, the constructor never needs to be called
What is the class signature when extending DiffUtil.ItemCallback?
class MyListObjectDiffCallback: DiffUtil.ItemCallback^MyListObject^()
What functions are implemented when extending DiffUtil.ItemCallback?
areItemsTheSame(oldItem: MyListObject, newItem: MyListObject): Boolean
areContentsTheSame(oldItem: MyListObject, newItem: MyListObject): Boolean
What is the role of a ListAdapter?
Keeps track of the list and notifies the Adapter when the list is updated
What is the class signature for implementing a ListAdapter?
class MyAdapter: ListAdapter^MyListObject, MyViewHolder^(MyListObjectDiffCallback( ) )
What can be removed from the Adapter when the ListAdapter is implemented?
The property keeping track of the list of data
The function getItemCount( )
Where does the function getItem(position) (provided by ListAdapter) need to be used?
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 do you use ListAdapter.submitList() ?
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^
What is the difference between inflating a ViewHolder View with and without data binding?
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 does the ViewHolder class signature change when implementing data binding?
- (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