MVVM Android视图未更新



我是Android MVVM的新手,使用livedata,创建文件夹结构作为模型,viewmodel&看法查看页面未更新,渐变内添加

def lifecycle_version = "2.3.1" 
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

变化正在发生,我可以在日志中看到,但变化并没有反映在视图中。我的型号类别

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;
}
}

ViewModel

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");
}
}
}

活动

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);
}
}

请帮我解决。

将其添加到创建的活动中

binding.lifecycleOwner= this

最新更新