Closed codeceo-net closed 3 years ago
package com.reagent.www.ui.user;
import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.databinding.ViewDataBinding; import androidx.lifecycle.ViewModelProviders;
import com.reagent.www.BR; import com.reagent.www.R; import com.reagent.www.app.AppViewModelFactory; import com.reagent.www.data.BaseRepository; import com.reagent.www.databinding.ActivityUserBinding; import com.reagent.www.ui.xpopup.XpopupViewModel; import com.spd.base.mvvm.base.BaseActivity; import com.spd.base.mvvm.base.BaseViewModel;
/**
@Version 1.0 */ public class UserActivity extends BaseActivity<ActivityUserBinding,UserViewModel> { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public int initContentView(Bundle savedInstanceState) { return R.layout.activity_user; } @Override public int initVariableId() { return BR.viewmodel; } @Override public UserViewModel initViewModel() { //使用自定义的ViewModelFactory来创建ViewModel,如果不重写该方法,则默认会调用LoginViewModel(@NonNull Application application)构造方法 AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication()); return ViewModelProviders.of(this, factory).get(UserViewModel.class); } @Override public void initData() { super.initData();
} @Override public void initViewObservable() { binding.tvSex.setText("nan"); } }
import android.app.Application;
import androidx.annotation.NonNull; import androidx.databinding.ObservableField;
import com.reagent.www.data.BaseRepository; import com.spd.base.mvvm.base.BaseViewModel;
@Version 1.0 */ public class UserViewModel extends BaseViewModel { public ObservableField titles = new ObservableField<>("wo sgi "); public UserViewModel(@NonNull Application application) { super(application); } public UserViewModel(@NonNull Application application, BaseRepository baseRepository ) { super(application); titles.set("ssss;ll");
}
@Override public void onCreate() { super.onCreate(); } @Override protected void onCleared() { super.onCleared(); } @Override public void onDestroy() { super.onDestroy(); } }
<?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:binding="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary"> <TextView android:id="@+id/tv_sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.titles}" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
代码如上 但是页面上的值为什么更新不了,对比其他页面没有错误呀,就是不更新
package com.reagent.www.ui.user;
import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.databinding.ViewDataBinding; import androidx.lifecycle.ViewModelProviders;
import com.reagent.www.BR; import com.reagent.www.R; import com.reagent.www.app.AppViewModelFactory; import com.reagent.www.data.BaseRepository; import com.reagent.www.databinding.ActivityUserBinding; import com.reagent.www.ui.xpopup.XpopupViewModel; import com.spd.base.mvvm.base.BaseActivity; import com.spd.base.mvvm.base.BaseViewModel;
/**
@Version 1.0 */ public class UserActivity extends BaseActivity<ActivityUserBinding,UserViewModel> { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public int initContentView(Bundle savedInstanceState) { return R.layout.activity_user; } @Override public int initVariableId() { return BR.viewmodel; } @Override public UserViewModel initViewModel() { //使用自定义的ViewModelFactory来创建ViewModel,如果不重写该方法,则默认会调用LoginViewModel(@NonNull Application application)构造方法 AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication()); return ViewModelProviders.of(this, factory).get(UserViewModel.class); } @Override public void initData() { super.initData();
} @Override public void initViewObservable() { binding.tvSex.setText("nan"); } }
package com.reagent.www.ui.user;
import android.app.Application;
import androidx.annotation.NonNull; import androidx.databinding.ObservableField;
import com.reagent.www.data.BaseRepository; import com.spd.base.mvvm.base.BaseViewModel;
/**
@Version 1.0 */ public class UserViewModel extends BaseViewModel {
public ObservableField titles = new ObservableField<>("wo sgi ");
public UserViewModel(@NonNull Application application) {
super(application);
}
public UserViewModel(@NonNull Application application, BaseRepository baseRepository ) {
super(application);
titles.set("ssss;ll");
}
@Override public void onCreate() { super.onCreate(); } @Override protected void onCleared() { super.onCleared(); } @Override public void onDestroy() { super.onDestroy(); } }
<?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:binding="http://schemas.android.com/apk/res-auto">
代码如上 但是页面上的值为什么更新不了,对比其他页面没有错误呀,就是不更新