我在Bitbucket中有以下分支:https : //bitbucket.org/ali-rezaei/tmdb/src/dataBinding/
Kotlin compiler
构建项目时出现以下错误:
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
错误与:
app:visibleGone="@{isLoaded}"
在以下布局中:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="isLoaded"
type="boolean" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:visibleGone="@{isLoaded}" />
</android.support.v4.widget.SwipeRefreshLayout>
<include
layout="@layout/network_state_item"
app:visibleGone="@{!isLoaded}" />
</FrameLayout>
</layout>
如果您能帮助我,我将不胜感激。
我要做的更改是:这里
<variable
name="isLoaded"
type="boolean" />
boolean
我没有传递,而是传递了您的VM实例
<variable
name="vm"
type="com.sample.android.tmdb.ui.MovieViewModel" />
在你的片段中
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false)
mBinding?.setVariable(BR.vm, mViewModel)
mBinding?.setLifecycleOwner(this)
这样,您的VM就可以连接到片段的生命周期。
声明方法
@BindingAdapter("visibleGone")
fun View.visibleGone(visible: Boolean) {
setVisibility(if (visible) View.VISIBLE else View.GONE)
}
LiveData<Boolean>
在您中声明一个变量MovieViewModel
并将其连接到您的布局中。例如。
val loading: LiveData<Boolean>
然后在您的布局中
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:visibleGone="@{!safeUnbox(vm.loading)}" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句