问题描述
升级到Gradle 2.3之后.我的项目无法编译.我在控制台中有日志
After upgrading to Gradle 2.3. My project cannot compile. I'm having the log in the console
incompatible types: ObservableInt cannot be converted to int
查看生成的文件
android.databinding.ObservableInt viewModelLoadingVisibility;
this.vLoading.getRoot().setVisibility(viewModelLoadingVisibility);
在xml
文件中
<android.support.v7.widget.RecyclerView
android:id="@+id/rvProducts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:visibility="@{viewModel.contentVisibility}"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
我在绑定类中尝试了方法
I tried in method in my binding class
@BindingAdapter("app:visibility")
public static void setViewVisible(View view, ObservableInt visible) {
int visibility = visible.get();
view.setVisibility(visibility);
}
并获得日志
public static void setViewVisible(View view, ObservableInt visible) {
public static void setViewVisible(View view, ObservableInt visible) {
有人遇到吗?
推荐答案
这看起来像个错误.请归档.有很多测试,我们不希望这种回归.重要的是,我们要为您提供具体的示例,以确保可以将其捕获.
This looks like a bug. Please file it. There are many tests and we don't expect this kind of regression. It is important that we get your specific example so we can be sure it is caught.
您现在可以忽略警告.
第一个是由于以下原因造成的:
The first is caused because of this:
@BindingAdapter("app:visibility")
您应该改用它:
@BindingAdapter("visibility")
第二个原因是因为我们支持ObservableInt
作为参数.通常,您不想接受ObservableInt
,而是希望接受int
.我希望看到需要ObservableInt
的用例.我们可能只是删除该警告并始终支持它,或者如果没有有效的使用,我们可能会拔掉支持ObservableInt
的插头作为参数.
The second is because we support ObservableInt
as a parameter. You typically don't want to accept ObservableInt
, but rather int
instead. I'd love to see use cases where ObservableInt
is necessary. We may just remove that warning and support it always or we may pull the plug on supporting ObservableInt
as a parameter if there are no valid uses.
-----编辑-----
----- edit -----
我用一个小的应用程序对此进行了测试,没有任何BindingAdapter,我就没有任何问题.这是布局:
I tested this with a little application and I didn't have any issue without any BindingAdapter. Here is the layout:
<layout>
<data>
<variable name="model" type="com.example.gmount.testobservableint.MyModel"/>
</data>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{model::clicked}"
tools:context="com.example.gmount.testobservableint.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="@{model.visibility}"
/>
</android.support.constraint.ConstraintLayout>
</layout>
这是我的模特:
public class MyModel {
public final ObservableInt visibility = new ObservableInt(View.VISIBLE);
public void clicked(View view) {
int oldVisibility = visibility.get();
int newVisibility = oldVisibility == View.VISIBLE ? View.GONE : View.VISIBLE;
visibility.set(newVisibility);
}
}
即使当我使用带有ObservableInt的BindingAdapter时,它也能正常工作.这是我的BindingAdapter:
Even when I used a BindingAdapter taking an ObservableInt, it worked. Here's my BindingAdapter:
@BindingAdapter("visiblity")
public static void setVisibility(View view, ObservableInt visibility) {
view.setVisibility(visibility.get());
}
然后我将View的绑定更改为:
And I changed the View's binding to be:
<TextView ...
app:visibility="@{model.visibility}"
/>
您的viewModel
是否有其他区别?
这篇关于升级到Gradle 2.3后,数据绑定中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!