如何通过数据绑定使用 sealed class 类型的可变实时数据
How to use mutable live data of type sealed class with data binding
我正在尝试使用具有数据绑定的密封类型 class 的可变 LiveData 来处理 ProgressBar 的可见性,但出现以下错误。
cannot find symbol variable sealed class
下面是我的代码
视图模型
class RevampSplashViewModel: RevampBaseViewModel() {
val splashEvents = MutableLiveData<SplashEvents>()
sealed class SplashEvents {
object Loading : SplashEvents()
object Success : SplashEvents()
data class Error(val message: String): SplashEvents()
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".revamp.ui.splash.RevampSplashActivity">
<data>
<import type="revamp.ui.splash.RevampSplashViewModel.SplashEvents"/>
<variable
name="vm"
type="revamp.ui.splash.RevampSplashViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
android:theme="@style/progressBarWhite"
android:visibility="@{vm.splashEvents == SplashEvents.Loading}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="@dimen/progressbar_dimen"
android:layout_height="@dimen/progressbar_dimen" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
下面是我面临的确切错误。
error: cannot find symbol
vmSplashEventsRevampUiSplashRevampSplashViewModelSplashEventsLoading = (vmSplashEventsGetValue) == (revamp.ui.splash.RevampSplashViewModel.SplashEvents.Loading);
error: illegal parenthesized expression
vmSplashEventsRevampUiSplashRevampSplashViewModelSplashEventsLoading = (vmSplashEventsGetValue) == (revamp.ui.splash.RevampSplashViewModel.SplashEvents.Loading);
我意识到我在比较不兼容的数据类型。解决方法是我们必须使用 instanceof 而不是 ==。下面是代码。
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".revamp.ui.splash.RevampSplashActivity">
<data>
<import type="revamp.ui.splash.RevampSplashViewModel.SplashEvents"/>
<variable
name="vm"
type="revamp.ui.splash.RevampSplashViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
android:theme="@style/progressBarWhite"
android:visibility="@{vm.splashEvents instanceof SplashEvents.Loading}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="@dimen/progressbar_dimen"
android:layout_height="@dimen/progressbar_dimen" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我正在尝试使用具有数据绑定的密封类型 class 的可变 LiveData 来处理 ProgressBar 的可见性,但出现以下错误。
cannot find symbol variable sealed class
下面是我的代码
视图模型
class RevampSplashViewModel: RevampBaseViewModel() {
val splashEvents = MutableLiveData<SplashEvents>()
sealed class SplashEvents {
object Loading : SplashEvents()
object Success : SplashEvents()
data class Error(val message: String): SplashEvents()
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".revamp.ui.splash.RevampSplashActivity">
<data>
<import type="revamp.ui.splash.RevampSplashViewModel.SplashEvents"/>
<variable
name="vm"
type="revamp.ui.splash.RevampSplashViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
android:theme="@style/progressBarWhite"
android:visibility="@{vm.splashEvents == SplashEvents.Loading}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="@dimen/progressbar_dimen"
android:layout_height="@dimen/progressbar_dimen" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
下面是我面临的确切错误。
error: cannot find symbol
vmSplashEventsRevampUiSplashRevampSplashViewModelSplashEventsLoading = (vmSplashEventsGetValue) == (revamp.ui.splash.RevampSplashViewModel.SplashEvents.Loading);
error: illegal parenthesized expression
vmSplashEventsRevampUiSplashRevampSplashViewModelSplashEventsLoading = (vmSplashEventsGetValue) == (revamp.ui.splash.RevampSplashViewModel.SplashEvents.Loading);
我意识到我在比较不兼容的数据类型。解决方法是我们必须使用 instanceof 而不是 ==。下面是代码。
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".revamp.ui.splash.RevampSplashActivity">
<data>
<import type="revamp.ui.splash.RevampSplashViewModel.SplashEvents"/>
<variable
name="vm"
type="revamp.ui.splash.RevampSplashViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
android:theme="@style/progressBarWhite"
android:visibility="@{vm.splashEvents instanceof SplashEvents.Loading}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="@dimen/progressbar_dimen"
android:layout_height="@dimen/progressbar_dimen" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>