我正在尝试通过数据绑定设置布局可见性。从数据库加载数据时,我在XML中设置的默认可见性不起作用。这是布局文件
<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
}
}
我在这里错过了什么吗?默认情况下,我想将错误布局的可见性设置为不可见。但默认情况下显示。
答案 0 :(得分:0)
使用visibility = invisible
或消失:
<RelativeLayout
android:id="@+id/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>
,然后在需要的地方通过编程方式更改可见性
if(TextUtils.isEmpty(some_error))
{
findViewbyId(R.id.error_layout).setVisibilty(View.VISIBILE)}
}
答案 1 :(得分:0)
Afaik,default
仅用于Android Studio上的预览,不会在运行时执行任何操作。找不到官方文档了,但是关于它的文章很多。
据我所知,设置绑定时,数据绑定框架将调用getComfortErrorVisibility
以获取错误消息的可见性。已设置您的条件,以便当错误消息为空或null
时可见性可见:
TextUtils.isEmpty(comfortErrorMessage.get()) ? View.VISIBLE : View.INVISIBLE;
由于comfortErrorMessage
的初始化方式类似于ObservableField()
,因此其初始值将为null,因此,您看到的第一件事就是可见的错误字段。
也许您应该更改可见性的条件?
答案 2 :(得分:0)
这是因为您在getComfortErrorVisibility
方法中犯了一个错误。开始时,您的comfortErrorMessage
为空,因此您的方法返回可见的a并显示文本视图,请尝试将您的方法更改为:
@Bindable
public int getComfortErrorVisibility(){
return TextUtils.isEmpty(comfortErrorMessage.get()) ? View.INVISIBLE: View.VISIBLE;
}
答案 3 :(得分:0)
public class HomeViewModel extends BaseObservable {
private ObservableField<String> comfortErrorMessage = new ObservableField<>();
public HomeViewModel(){
validateSpace();
}
}
在您的<data>
绑定标记中导入文本工具,然后...
<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"/>
答案 4 :(得分:0)
添加您的comfortErrorVisibility
,将其初始化为View.INVISIBLE
private ObservableField<String> comfortErrorMessage = new ObservableField<>();
private ObservableField<Integer> comfortErrorVisibility = new ObservableField<>(View.INVISIBLE);
在您的XML中,只需输入字段名称:
<RelativeLayout
android:id="@+id/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@{homeViewModel.comfortErrorVisibility}"/>
如果您的comfortErrorMessage
可以在运行时动态更改,即用户可以从EditText
对其进行编辑,那么您可以添加属性更改的回调
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
}
}
,并且您的comfortErrorMessage
应该分配带有=
符号"@={homeViewModel.comfortErrorMessage}"