【发布时间】:2019-08-21 19:35:09
【问题描述】:
我正在尝试使用示例项目 android-sunflower 对视图模型进行数据绑定。当前的问题是,当我尝试构建项目时,我在 DataBindinMapperImpl 类中收到错误 error: cannot find symbol symbol: class FragmentShopBindingImpl
location: package {{packageName}}.databinding
我不确定我在这里缺少什么,因为我添加了示例项目中的所有内容。 FragmentShopBindingImpl 类没有生成,或者不应该生成吗?由于我在 android sunflower 示例中看不到任何以“Impl”结尾的类
我的代码:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val factory = InjectorUtils.provideShopViewModelFactory(context!!)
val shopViewModel = ViewModelProviders.of(this, factory)
.get(ShopViewModel::class.java)
val binding = DataBindingUtil.inflate<FragmentShopBinding>(
inflater, R.layout.fragment_shop, container, false).apply {
viewModel = shopViewModel
lifecycleOwner = this@ShopFragment
}
return binding.root
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="{{packageName}}.viewmodel.ShopViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragments.ShopFragment">
<TextView
android:text="@{viewModel}"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
生成文件的图像(忽略 {{packageName}}:
【问题讨论】:
标签: android kotlin data-binding android-databinding android-jetpack