1
下面運行該代碼與Android版本SDK 16.但是一個機器人仿真器以某種方式,當它運行在與Android版本> 21,數據綁定與境界模擬器儲蓄不被觸發..的DatePicker與數據綁定和Rxjava可觀測視圖不保存到境界上不同的Android版本
我應該如何與Android版本來處理?
是因爲所呈現的UI在不同的Android版本不同,所以數據綁定不會有任何的工作?
這裏這段代碼視圖模型
@PerFragment
public class OnboardDOBViewModel extends BaseHelperViewModel<BaseUserView> implements IOnboardDOBViewModel {
private String TAG = getClass().getSimpleName();
private HelperRepo mHelperRepo;
private UserRepo mUserRepo;
private Helper mHelper;
public final ObservableField<Integer> datePicker = new ObservableField<>();
public final ObservableField<Integer> monthPicker = new ObservableField<>();
public final ObservableField<Integer> yearPicker = new ObservableField<>();
public ReadOnlyField dobSelected;
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
@Inject
public OnboardDOBViewModel(@AppContext Context context, UserRepo userRepo, HelperRepo helperRepo) {
super(context, userRepo, helperRepo);
Log.d(TAG, "OnboardDOBViewModel: ");
this.mUserRepo = userRepo;
this.mHelperRepo = helperRepo;
}
@Override
public void attachView(BaseUserView view, @Nullable Bundle savedInstanceState) {
super.attachView(view, savedInstanceState);
Log.d(TAG, "attachView");
mHelper = mHelperRepo.getByUser("user.id", mUserRepo.getCurrentUser(), false);
if (mHelper != null) {
Calendar c = Calendar.getInstance();
yearPicker.set(c.get(Calendar.YEAR)-23);
monthPicker.set(c.get(Calendar.MONTH));
datePicker.set(c.get(Calendar.DAY_OF_MONTH));
}
Observable<String> dobSelected = Observable.combineLatest(
Utils.toObservable(monthPicker),
Utils.toObservable(datePicker),
Utils.toObservable(yearPicker),
(integer, integer2, integer3) -> {
String dobSelected1 = String.valueOf(monthPicker.get() + 1) + "-" +
datePicker.get().toString() + "-" +
yearPicker.get().toString();
Log.d(TAG, "apply: " + dobSelected1);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy", Locale.getDefault());
Date date = sdf.parse(dobSelected1);
Log.d(TAG, "apply: date selected" + date);
Helper helper = mHelperRepo.getByUser("user.id", mUserRepo.getCurrentUser(), true);
assert helper != null;
helper.setDateOfBirth(date);
mHelperRepo.save(helper);
return dobSelected1;
});
this.dobSelected = Utils.toField(dobSelected);
}
這是發現這個作爲我的答案XML
<data>
<variable
name="vm"
type="...OnboardDOBViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="30dp"
android:text="@string/toolbar_title_date_of_birth"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<DatePicker
android:id="@+id/onboard_birth_date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false"
android:day="@={vm.datePicker}"
android:month="@={vm.monthPicker}"
android:solidColor="@color/grayMedium"
android:spinnersShown="true"
android:year="@={vm.yearPicker}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="test"
android:text="@{vm.dobSelected}"
android:visibility="visible"/>
</LinearLayout>