Android Progressbar数据绑定can';不要改变能见度
我在Android中的布局中有一个progressbar,我想更改它在viewmodel中的可见性。但应用程序只获得progressbar的第一个状态。每当我试图改变它,什么都不会发生。这是我的布局文件Android Progressbar数据绑定can';不要改变能见度,android,data-binding,Android,Data Binding,我在Android中的布局中有一个progressbar,我想更改它在viewmodel中的可见性。但应用程序只获得progressbar的第一个状态。每当我试图改变它,什么都不会发生。这是我的布局文件 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"
<?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>
<import type="android.view.View"></import>
<variable
name="viewmodel"
type="com.tolgahantutar.bexworkfloww.ui.auth.AuthViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="24dp"
android:paddingTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="154dp"
android:layout_height="154dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="48dp"
android:src="@drawable/bexfalogo"
android:contentDescription="@string/bexlogo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="132dp"
android:textAllCaps="true"
android:textSize="16dp" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/hint_username">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_text_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={viewmodel.userName}"
>
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/hint_password"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="@={viewmodel.password}"
/>
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="@string/button_login"
android:onClick="@{viewmodel::onClickUserLogin}"
/>
<com.google.android.material.button.MaterialButton
android:id="@+id/button_cancel"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_toStartOf="@id/button_login"
android:layout_toLeftOf="@id/button_login"
android:text="@string/button_cancel" />
</RelativeLayout>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="@{viewmodel.isLoading ? View.VISIBLE : View.GONE}" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我的viewmodel看起来像这样
package com.tolgahantutar.bexworkfloww.ui.auth
import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.ObservableBoolean
import androidx.databinding.library.baseAdapters.BR
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tolgahantutar.bexworkfloww.data.network.repositories.AuthorizeSessionRepository
import com.tolgahantutar.bexworkfloww.ui.home.HomeActivity
import com.tolgahantutar.bexworkfloww.util.ApiException
import com.tolgahantutar.bexworkfloww.util.NoInternetException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.internal.notify
class AuthViewModel (
private val repository: AuthorizeSessionRepository
):ViewModel() {
var userName: String?=null
var password: String?=null
val isLoading = MutableLiveData<Boolean>(true)
fun onClickUserLogin(view: View){
isLoading.value = false
val sessionID = 0
val authorityID = 0
val loginType = "System"
viewModelScope.launch {
if(!(userName==null||password==null)){
try{
val authResponse = userLogin(sessionID,authorityID,userName!!,password!!,loginType)
if(authResponse.Result){
isLoading.value=false
Toast.makeText(view.context, "Login Successfull", Toast.LENGTH_LONG).show()
val intent = Intent(view.context,HomeActivity::class.java)
view.context.startActivity(intent)
}else{
//isLoading.value=false
Toast.makeText(view.context, "Login Failed!!", Toast.LENGTH_LONG).show()
}
}catch (e: ApiException){
e.printStackTrace()
}catch (e: NoInternetException){
e.printStackTrace()
}}
else{
Toast.makeText(view.context, "Kullanıcı adı ve şifre boş bırakılamaz!!", Toast.LENGTH_SHORT).show()
}
}
}
suspend fun userLogin(
SessionID : Int,
AuthorityID: Int,
UserName: String,
Password : String,
LoginType: String
)= withContext(Dispatchers.IO){repository.userLogin(SessionID, AuthorityID, UserName, Password, LoginType)}
}
package com.tolgahantutar.bexworkfloww.ui.auth
导入android.content.Intent
导入android.view.view
导入android.widget.Toast
导入androidx.databinding.BaseObservable
导入androidx.databinding.Bindable
导入androidx.databinding.ObservableBolean
导入androidx.databinding.library.baseAdapters.BR
导入androidx.lifecycle.MutableLiveData
导入androidx.lifecycle.ViewModel
导入androidx.lifecycle.viewModelScope
导入com.tolgahantutar.bexworkfloww.data.network.repositories.AuthorizeSessionRepository
导入com.tolgahantutar.bexworkfloww.ui.home.HomeActivity
导入com.tolgahantutar.bexworkfloww.util.ApiException
导入com.tolgahantutar.bexworkfloww.util.NoInternetException
导入kotlinx.coroutines.Dispatchers
导入kotlinx.coroutines.launch
导入kotlinx.coroutines.withContext
导入okhttp3.internal.notify
类AuthViewModel(
私有val存储库:AuthorizeSessionRepository
):ViewModel(){
变量用户名:字符串?=null
var密码:字符串?=null
val isLoading=MutableLiveData(真)
有趣的onClickUserLogin(视图:视图){
isLoading.value=false
val sessionID=0
val authorityID=0
val loginType=“系统”
viewModelScope.launch{
如果(!(用户名==null | |密码==null)){
试一试{
val authResponse=userLogin(sessionID,authorityID,userName!!,password!!,loginType)
if(authResponse.Result){
isLoading.value=false
Toast.makeText(view.context,“Login Successfull”,Toast.LENGTH\u LONG.show())
val intent=intent(view.context,HomeActivity::class.java)
view.context.startActivity(意图)
}否则{
//isLoading.value=false
Toast.makeText(view.context,“登录失败!!”,Toast.LENGTH\u LONG.show()
}
}捕获(e:异常){
e、 printStackTrace()
}捕获(e:NoInternetException){
e、 printStackTrace()
}}
否则{
Toast.makeText(view.context,“Kullanıcıadıveşifre boşbırakılamaz!!”,Toast.LENGTHıu SHORT.show()
}
}
}
暂停用户登录(
会话ID:Int,
AuthorityID:Int,
用户名:String,
密码:String,
登录类型:字符串
)=withContext(Dispatchers.IO){repository.userLogin(SessionID,AuthorityID,UserName,Password,LoginType)}
}
正如我提到的,它只是获取第一个状态,但当我单击按钮时,没有任何变化。您永远不会再次将isLoading设置为true,请检查此项。这无关紧要。我的意思是,这段代码应该在开始时显示progressbar(它显示),并在单击onClickUserLogin()后使其不可见。但它并没有使我单击后它不可见。它看起来一切正常,是否调用了方法
onClickUserLogin()
?我的意思是,您是否正确地从片段/活动调用了viewModel?是的,我在数据绑定中使用了这种方法。我将ViewModel绑定到布局。我从ViewModel中调用它。我通过观察活动中的孤岛加载找到了一种方法。它工作得很好,但我想知道为什么这种方式不起作用