MVVM Android 视图未更新
MVVM Android view not updating
我是 Android 具有实时数据的 MVVM 的新手,创建了文件夹结构作为模型、视图模型和视图
查看页面未更新,在 gradle 内已添加
def lifecycle_version = "2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
更改正在发生,我可以在日志中看到,但更改没有反映在视图中。
我的模型 class
public class SplashScreenModel {
private String message;
private Boolean inProgress;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Boolean getInProgress() {
return inProgress;
}
public void setInProgress(Boolean inProgress) {
this.inProgress = inProgress;
}
}
视图模型
public class SplashScreenViewModel extends BaseViewModel {
//#region VARIABLES
public static final String TAG="SPLASH VM";
private ConfigRepository configRepository;
private MyAsyncTask myAsyncTask;
private MutableLiveData<String> message = new MutableLiveData<>();
public MutableLiveData<String> getMessage() {
return message;
}
public void setMessage(String message){
this.message.setValue(message);
}
public void showMessage(String s){
message.setValue(s);
}
//#endregion
public SplashScreenViewModel(@NonNull Application application) {
super(application);
configRepository = new ConfigRepository(application);
myAsyncTask = new MyAsyncTask();
}
public void test(){
myAsyncTask.execute();
}
private class MyAsyncTask extends AsyncTask<Void,Integer,Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
for (int n=1; n<=10; n++){
Log.d(TAG, "background: Loop "+n);
Thread.sleep(500);
publishProgress(n);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
showMessage(String.valueOf(values[0]));
}
@Override
protected void onPostExecute(Void unused) {
super.onPostExecute(unused);
Log.d(TAG, "onPostExecute: Executed");
showMessage("Done");
}
}
}
Activity
public class SplashActivity extends AppCompatActivity {
//#region VARIABLE
private static final String TAG="SPLASH";
private SplashScreenViewModel splashViewModel;
private String text ="Begins";
//TextView textView;
ActivitySplashBinding binding;
//#endregion
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
binding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
//textView = findViewById(R.id.lbl_splash_message);
splashViewModel = new ViewModelProvider(this).get(SplashScreenViewModel.class);
splashViewModel.showMessage("abhi waiting...");
//binding.lblSplashMessage.setText(text);
splashViewModel.getMessage().observe(this, s -> {
//Toast.makeText(SplashActivity.this,s,Toast.LENGTH_SHORT).show();
Log.d(TAG, "onChanged: "+s);
String d = splashViewModel.getMessage().getValue();
Log.d(TAG, "message value: "+d);
binding.lblSplashMessage.setText(d); // by doing like am getting desired result.
// but how it automatically reflect on view.
});
splashViewModel.test();
}
}
启动画面布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="splashViewModel"
type="com.fonz.pos_quick_pay.ui.main.viewmodel.SplashScreenViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/SplashTheme"
tools:context=".ui.main.view.SplashActivity">
<ProgressBar
android:id="@+id/progress_splash"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toTopOf="@id/lbl_splash_message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/lbl_splash_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="120dp"
android:text="@{splashViewModel.message}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
基本视图模型
public abstract class BaseViewModel extends AndroidViewModel {
private final ObservableBoolean mIsLoading = new ObservableBoolean();
public BaseViewModel(@NonNull Application application) {
super(application);
}
public ObservableBoolean getIsLoading() {
return mIsLoading;
}
public void setIsLoading(boolean isLoading) {
mIsLoading.set(isLoading);
}
}
请帮我解决
在创建时将此添加到您的 activity
binding.lifecycleOwner= this
我是 Android 具有实时数据的 MVVM 的新手,创建了文件夹结构作为模型、视图模型和视图 查看页面未更新,在 gradle 内已添加
def lifecycle_version = "2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
更改正在发生,我可以在日志中看到,但更改没有反映在视图中。 我的模型 class
public class SplashScreenModel {
private String message;
private Boolean inProgress;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Boolean getInProgress() {
return inProgress;
}
public void setInProgress(Boolean inProgress) {
this.inProgress = inProgress;
}
}
视图模型
public class SplashScreenViewModel extends BaseViewModel {
//#region VARIABLES
public static final String TAG="SPLASH VM";
private ConfigRepository configRepository;
private MyAsyncTask myAsyncTask;
private MutableLiveData<String> message = new MutableLiveData<>();
public MutableLiveData<String> getMessage() {
return message;
}
public void setMessage(String message){
this.message.setValue(message);
}
public void showMessage(String s){
message.setValue(s);
}
//#endregion
public SplashScreenViewModel(@NonNull Application application) {
super(application);
configRepository = new ConfigRepository(application);
myAsyncTask = new MyAsyncTask();
}
public void test(){
myAsyncTask.execute();
}
private class MyAsyncTask extends AsyncTask<Void,Integer,Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
for (int n=1; n<=10; n++){
Log.d(TAG, "background: Loop "+n);
Thread.sleep(500);
publishProgress(n);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
showMessage(String.valueOf(values[0]));
}
@Override
protected void onPostExecute(Void unused) {
super.onPostExecute(unused);
Log.d(TAG, "onPostExecute: Executed");
showMessage("Done");
}
}
}
Activity
public class SplashActivity extends AppCompatActivity {
//#region VARIABLE
private static final String TAG="SPLASH";
private SplashScreenViewModel splashViewModel;
private String text ="Begins";
//TextView textView;
ActivitySplashBinding binding;
//#endregion
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
binding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
//textView = findViewById(R.id.lbl_splash_message);
splashViewModel = new ViewModelProvider(this).get(SplashScreenViewModel.class);
splashViewModel.showMessage("abhi waiting...");
//binding.lblSplashMessage.setText(text);
splashViewModel.getMessage().observe(this, s -> {
//Toast.makeText(SplashActivity.this,s,Toast.LENGTH_SHORT).show();
Log.d(TAG, "onChanged: "+s);
String d = splashViewModel.getMessage().getValue();
Log.d(TAG, "message value: "+d);
binding.lblSplashMessage.setText(d); // by doing like am getting desired result.
// but how it automatically reflect on view.
});
splashViewModel.test();
}
}
启动画面布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="splashViewModel"
type="com.fonz.pos_quick_pay.ui.main.viewmodel.SplashScreenViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/SplashTheme"
tools:context=".ui.main.view.SplashActivity">
<ProgressBar
android:id="@+id/progress_splash"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toTopOf="@id/lbl_splash_message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/lbl_splash_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="120dp"
android:text="@{splashViewModel.message}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
基本视图模型
public abstract class BaseViewModel extends AndroidViewModel {
private final ObservableBoolean mIsLoading = new ObservableBoolean();
public BaseViewModel(@NonNull Application application) {
super(application);
}
public ObservableBoolean getIsLoading() {
return mIsLoading;
}
public void setIsLoading(boolean isLoading) {
mIsLoading.set(isLoading);
}
}
请帮我解决
在创建时将此添加到您的 activity
binding.lifecycleOwner= this