Android 具有数据绑定的布局可见性默认值无效
我正在尝试使用数据绑定设置布局可见性。从数据库加载数据时,我在XML中设置的默认可见性不起作用。这是布局文件Android 具有数据绑定的布局可见性默认值无效,android,android-layout,android-databinding,Android,Android Layout,Android Databinding,我正在尝试使用数据绑定设置布局可见性。从数据库加载数据时,我在XML中设置的默认可见性不起作用。这是布局文件 <RelativeLayout android:id="@+id/error_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="@{ho
<RelativeLayout
android:id="@+id/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{homeViewModel.comfortErrorVisibility, default=invisible}"/>
视图模型如下所示
public class HomeViewModel extends BaseObservable {
private ObservableField<String> comfortErrorMessage = new ObservableField<>();
public HomeViewModel(){
validateSpace();
}
@Bindable
public int getComfortErrorVisibility(){
// change the visibility based on error message
return TextUtils.isEmpty(comfortErrorMessage.get()) ? View.VISIBLE : View.INVISIBLE;
}
private void validateSpace(){
//some business logic to set the comfrotErrorMessage
}
}
公共类HomeViewModel扩展了BaseObservable{
私有ObserverField comfortErrorMessage=新ObserverField();
公共HomeViewModel(){
验证空间();
}
@装订的
public int getComfortErrorVisibility(){
//根据错误消息更改可见性
返回text utils.isEmpty(comfortErrorMessage.get())?View.VISIBLE:View.INVISIBLE;
}
私有void validateSpace(){
//设置通信消息的一些业务逻辑
}
}
我有什么遗漏吗?默认情况下,我希望将错误布局的可见性设置为不可见。但它默认显示。使用
可见性=不可见或消失:
<RelativeLayout
android:id="@+id/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>
好吧,default
只是在Android studio上预览,在运行时不会做任何事情。再也找不到官方文件了,但是有很多关于它的帖子
据我所知,当您设置绑定时,数据绑定框架将调用getComfortErrorVisibility
以获取错误消息的可见性。您的条件设置为,当错误消息为空或null
时,可见性可见:
TextUtils.isEmpty(comfortErrorMessage.get()) ? View.VISIBLE : View.INVISIBLE;
由于您的comfortErrorMessage
像ObservableField()
一样初始化,其初始值将为null,因此您首先看到的是一个可见的错误字段
也许您应该更改可见性的条件?这是因为您在getComfortErrorVisibility
方法中犯了错误。开始时,您的comfortErrorMessage
为空,因此您的方法返回可见的文本视图,请尝试将您的方法更改为:
@Bindable
public int getComfortErrorVisibility(){
return TextUtils.isEmpty(comfortErrorMessage.get()) ? View.INVISIBLE: View.VISIBLE;
}
公共类HomeViewModel扩展了BaseObservable{
私有ObserverField comfortErrorMessage=新ObserverField();
公共HomeViewModel(){
验证空间();
}
}
导入
绑定标记中的文本UTIL并
<RelativeLayout
android:id="@+id/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{TextUtils.isEmpty(viewmodel.comfortErrorMessage) ? View.VISIBLE : View.INVISIBLE"/>
添加您的comfortErrorVisibility
,将其初始化为View.INVISIBLE
private ObservableField<String> comfortErrorMessage = new ObservableField<>();
private ObservableField<Integer> comfortErrorVisibility = new ObservableField<>(View.INVISIBLE);
您的comfortErrorMessage
应该具有双向绑定,并带有=
符号“@={homeViewModel.comfortErrorMessage}”
谢谢您的回复。但是,我使用数据绑定概念来更新UI。这是不可行的。
<RelativeLayout
android:id="@+id/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{TextUtils.isEmpty(viewmodel.comfortErrorMessage) ? View.VISIBLE : View.INVISIBLE"/>
private ObservableField<String> comfortErrorMessage = new ObservableField<>();
private ObservableField<Integer> comfortErrorVisibility = new ObservableField<>(View.INVISIBLE);
<RelativeLayout
android:id="@+id/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{homeViewModel.comfortErrorVisibility}"/>
import android.text.TextUtils;
import android.view.View;
import androidx.databinding.BaseObservable;
import androidx.databinding.Observable;
import androidx.databinding.ObservableField;
public class HomeViewModel extends BaseObservable {
private ObservableField<String> comfortErrorMessage = new ObservableField<>();
private ObservableField<Integer> comfortErrorVisibility = new ObservableField<>(View.INVISIBLE);
public HomeViewModel() {
validateSpace();
comfortErrorMessage.addOnPropertyChangedCallback(
new Observable.OnPropertyChangedCallback() {
@Override
public void onPropertyChanged(Observable sender, int propertyId) {
if (TextUtils.isEmpty(comfortErrorMessage.get())) {
comfortErrorVisibility.set(View.VISIBLE);
} else {
comfortErrorVisibility.set(View.INVISIBLE);
}
}
}
);
}
private void validateSpace() {
//some business logic to set the comfrotErrorMessage
}
}