【发布时间】:2021-01-28 04:40:55
【问题描述】:
我想在our project 中开始使用viewBinding,但是仅仅添加配置就会导致编译错误:
android {
buildFeatures {
dataBinding true
viewBinding true // new line and only change
}
结果:
e: /home/leo/StudioProjects/android-wallet/mbw/build/generated/source/kapt/btctestnetDebug/com/mycelium/wallet/DataBinderMapperImpl.java:37: error: cannot find symbol
import com.mycelium.wallet.databinding.FragmentBequantAccountBindingImpl;
^
symbol: class FragmentBequantAccountBindingImpl
location: package com.mycelium.wallet.databinding
Cannot find a setter for <com.mycelium.wallet.databinding.ItemBequantSearchBinding app:visibility> that accepts parameter type 'int'
If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
违规代码是:
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="com.mycelium.bequant.market.viewmodel.AccountViewModel" />
</data>
...
<include
android:id="@+id/searchBar"
layout="@layout/item_bequant_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}" removing="this line fixes compilation"
app:layout_constraintTop_toBottomOf="@id/hideZeroBalance" />
将违规行更改为任何一个
android:visibility="@{viewModel.searchMode ? `visible` : `gone`}"
app:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}"
导致类似的错误。
我读到我可能必须定义一个BindingAdapter,但为什么以及在哪里?
我尝试添加
@BindingAdapter("visibility")
fun setVisibility(target: View, visible: Boolean) {
target.visibility = if (visible) View.VISIBLE else View.GONE
}
到AccountFragment,它会膨胀上面的xml文件,将xml更改为
android:visibility="@{viewModel.searchMode}"
但这似乎没有效果。
fragment_bequant_account.xml 和 item_bequant_search.xml use androidx.constraintlayout.widget.ConstraintLayout instead of androidx.constraintlayout.ConstraintLayout。
我尝试将@BindingAdapter 放入AccountViewModel as suggested here 但没有成功。
【问题讨论】:
-
您的数据绑定之前是否有效,或者您也添加了该内容?
-
数据绑定在多次提交后工作。数百个。添加标有
// new line and only change的行后出现错误。 -
数据绑定已经包含 ViewBinding。无需添加该行。 developer.android.com/topic/libraries/view-binding#data-binding
-
在链接的文档中,我没有看到任何关于其中一个的声明,但我看到
tools:viewBindingIgnore="true"添加到一个有问题的布局中解决了我的问题,所以感谢您让我阅读该文档!!!我很高兴,但仍然很好奇为什么这个visibility不像其他人。
标签: android android-viewbinding