问题
I'm trying to use DataBinding
for a ProgressBar
, but I didn't know how
this is my xml code
<?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>
as you see I added the
layout, data and variable to xml code
What I have to do in the model now?
Thank you so much <3
回答1:
For progressBar you need to do the following:
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>
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.
To make it work smoothly, you should make it like Observable. you can do so by making your model extends BaseObservable
public class yourmodel extends BaseObservable
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);
}
Note: You may need to build your project to generate BR file.
You can check this model for file more details
Your isLoading field now should be Observable
Thirdly:
You should add the visibility attribute based on the value of isLoading
.
<ProgressBar
..........
..........
android:visibility="@{addActivity.isLoading ? View.VISIBLE : View.GONE}"/>
I hope this would help.
来源:https://stackoverflow.com/questions/60100306/how-can-i-use-data-binding-with-progressbar