问题描述
我正在尝试将 DataBinding
用于 ProgressBar
,但是我不知道如何
I'm trying to use DataBinding
for a ProgressBar
, but I didn't know how
这是我的xml代码
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="addActivity"
type="com.khairo.dallasadmin.view.addScreenSaver.AddScreenSaver" />
<variable
name="saverModel"
type="com.khairo.dallasadmin.model.addScreenSaver.AddScreenSaverModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.addScreenSaver.AddScreenSaver">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image"
android:layout_width="@dimen/_130sdp"
android:layout_height="@dimen/_230sdp"
android:src="@drawable/ic_add"
android:onClick="@{()->addActivity.selectImage()}"
android:layout_marginTop="@dimen/_25sdp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_35sdp"
android:text="@string/send_image"
android:background="@drawable/carve_blue"
android:textStyle="bold"
android:textColor="@color/white"
android:onClick="@{()->addActivity.sendImage()}"
android:textSize="@dimen/_11ssp"
android:layout_margin="@dimen/_15sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
如您所见,我添加了
我现在必须在模型中做什么?
What I have to do in the model now?
非常感谢< 3
Thank you so much <3
推荐答案
对于progressBar,您需要执行以下操作:
For progressBar you need to do the following:
第一:您应该导入视图,以便能够在数据标签内处理progressBar的可见性,如下所示:
First:You should import View to be able to handle visibility of your progressBar within your data tag as below:
<data>
.....
import type="android.view.View"/>
<data>
其次:您应该将 isLoading 变量添加到布尔类型的模型中.此 isLoading 变量会根据您的业务进行更改,因此当您需要您的progressBar时,可以将其保持为 true .
Secondly:You should add isLoading variable to your Model of type Boolean. This isLoading variable is changed according to your business, so when you need your progressBar is visible you keep it as true.
要使其平稳运行,应使其像可观察的"一样.您可以通过使模型扩展BaseObservable来实现
To make it work smoothly, you should make it like Observable. you can do so by making your model extends BaseObservable
然后应为 isLoading 生成getter和setter,如下所示:
then you should generate getter and setter for isLoading as follows:
@Bindable
public boolean isLoading() {
return isLoading;
}
public void setLoading(boolean loading) {
isLoading = loading;
notifyPropertyChanged(BR.loading);
}
注意:您可能需要构建项目以生成 BR 文件.
Note: You may need to build your project to generate BR file.
您可以在文件更多详细信息
您的正在加载字段现在应该是可观察的
Your isLoading field now should be Observable
第三次:
您应该基于 isLoading
的值添加可见性属性.
You should add the visibility attribute based on the value of isLoading
.
<ProgressBar
..........
..........
android:visibility="@{addActivity.isLoading ? View.VISIBLE : View.GONE}"/>
我希望这会有所帮助.
这篇关于如何将数据绑定与ProgressBar一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!