Question: What will be the scope of View Model object if it is intialized directly without using ViewModelProvider inside Adapter class ?
PlantAndGardenPlantingsViewModel object is initialized directly inside GardenPlantingAdapter. How will observable fields of this ViewModel behave?
would you mind address some details, I cannot catch?
Sorry for the inconvenience. I am pasting code snippet below for further details. My question is regarding the use of PlantAndGardenPlantingsViewModel(itemView.context, plantings) code statement.
Question : I have only seen View Model class getting initialized in Activity/Fragment using "ViewModelProviders.of" statement, but here in below code snippet View Model class (PlantAndGardenPlantingsViewModel in our case) is initialized inside a Adapter class (GardenPlantingAdapter). Not sure how Observable fields of this ViewModel class will work with DataBinding. Also, what is the purpose of creating multiple view model objects one for each item in Adapter list?
`class GardenPlantingAdapter( val context: Context ) : ListAdapter<PlantAndGardenPlantings, GardenPlantingAdapter.ViewHolder>(GardenPlantDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.list_item_garden_planting, parent, false
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
getItem(position).let { plantings ->
with(holder) {
itemView.tag = plantings
bind(plantings)
}
}
}
class ViewHolder(
private val binding: ListItemGardenPlantingBinding
) : RecyclerView.ViewHolder(binding.root) {
fun bind(plantings: PlantAndGardenPlantings) {
with(binding) {
viewModel = PlantAndGardenPlantingsViewModel(
itemView.context,
plantings
)
executePendingBindings()
}
}
}
}`
Yes, actually you are right, even the google hasn't a solution for ViewModel of Item which should be created from a factory. However, I think the ViewModel of Item will be considered as a normal object of data-binding. The idea of MVVM is that everything of business could and should be done in the ViewModel including the Items, never expose logical to UI level.
Thanks for the reply. I have a quick question. Sunflower is recommended as one of the best starter guide for learning android architecture components. As we know it is in development phase, where can we get the roadmap of upcoming features? Very few google github repos in kotlin are available for Android Jetpack components that address a specific area only.
Sunflower is eagerly awaited app, which provides end to end solution. As of now, Sunflower is not using all Architecture component and even for the existing one's full power of Architecture components is not demonstrated as compared to other google codelabs. It's humble request to include more screens and features and if possible share roadmap for upcoming features.
...and to provide a working example of master-detail flow using either ConstraintSets, or Navigation AAC :smile: