在片段中使用以下方法初始化绑定
val binding = FragmentReportDetailsBinding.inflate(inflater).apply {
setLifecycleOwner(this@ReportDetailsFragment)
}
并添加viewModel:
val viewModel = ViewModelProviders.of(
this, ReportViewModelFactory(month, year)
).get(ReportViewModel::class.java)
binding.viewModel = viewModel
viewModel.obtainData()
在viewModel中,我设置以下值:
var hours: ObservableInt = ObservableInt(0)
fun obtainData() {
hours.set(3)
}
但是从视图上看,它什么都没显示。
<TextView
android:id="@+id/timeTitle"
android:text="@{String.valueOf(viewModel.hours)}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
/>