Sign in Go Pro

Android Model-View-ViewModel Pattern

Creating An Alert Dialog with a RecyclerView - Part 2

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

Up next

Previous

About

In this lesson, we'll finish what we started, adding a RecyclerView Adapter for our LoadTipsDialog to prepare and bind the TipCalculationSummaryItem DTOs we created in Part 1. As we make our way to the finish line, you'll learn how to access the ViewModel instance from our Fragment too and fetch some LiveData from it to feed into our RecyclerView.

Key concepts in this lesson that you will learn:

  • How to build a RecyclerView Adapter for a Data Bound View (a view which is using DataBinding)
  • Using Kotlin Lambda's to pass item click events out of the Adapter, back to the owner of the RecyclerView.
  • Getting a reference to the same shared AndroidViewModel instance between your Activities and Fragments.
  • How the LiveData Architecture Components - Transformations.map(...) works
  • Using a RecyclerView, with DataBinding and LiveData. This lesson shows you specifically how to tie all three of these together.

Instructor

Links

Comments

In the loadTipCalculation function, you call the notifyChange function even though the updateOutputs function already makes a call to it.

Hi Kwabena,

That's a great point!

I chose to keep notifiyChange() inside of updateOutputs(tc: TipCalculation) because I call it from 4 different places in the viewModel and each time, always want to update the view after setting.

I also call notifyChange() from loadTipCalculation, because I set inputCheckAmount and inputTipPercentage, but to your point, these would be updated anyway as a side effect of calling updateOutputs(tc: Tip Calculation) from that method.

One thing I could do here is pass a boolean into updateOutputs indicating whether to notify change. Something like this.


private fun updateOutputs(tc: TipCalculation, notify: Boolean = true) {
lastTipCalculated = tc
if(notify) { notifyChange() }
}

In that way, I could rely on the default most of the time, but override where it makes sense, like in loadTipCalculation, where I can be more explicit with the call and more efficient.

Thanks!
Eric

>
You need to go PRO to post comments.

Lessons in Android Model-View-ViewModel Pattern