RxBinding TextChanges 不会继续发出文本更改
RxBinding TextChanges won't continue emitting text changes
试图找出为什么 RxTextView.textChanges 只发出初始值。我的理解是它应该向下游订阅发送所有新的文本更改。
这是代码
public class SignupFragment extends Fragment {
@BindView(R.id.text_name) EditText txtName;
@BindView(R.id.text_email) EditText txtEmail;
@BindView(R.id.text_password) EditText txtPassword;
@BindView(R.id.text_confirm_password) EditText txtConfirmPassword;
@BindView(R.id.btn_signup) Button btnSignup;
SignupViewModel viewModel;
private CompositeDisposable disposables;
FragmentSignupBinding binding;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signup, container, false);
ButterKnife.bind(this, view);
this.disposables = new CompositeDisposable();
RxTextView.textChanges(txtEmail)
.subscribe(new Observer<CharSequence>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(CharSequence charSequence) {
Log.d("Subscription", charSequence.toString());
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.d("Subscription", "On Complete");
}
});
viewModel = ViewModelProviders.of(this).get(SignupViewModel.class);
viewModel.applyInputs(RxTextView.textChanges(txtName),
RxTextView.textChanges(txtEmail),
RxTextView.textChanges(txtPassword),
RxTextView.textChanges(txtConfirmPassword),
RxView.clicks(btnSignup));
Disposable validInputs =
viewModel.validInputs()
.doOnNext(new Consumer<Boolean>() {
@SuppressLint("TimberArgCount")
@Override
public void accept(Boolean aBoolean) throws Exception {
Timber.d("Valid inputs: %b", aBoolean.booleanValue());
}
})
.subscribe(RxView.visibility(btnSignup));
disposables.add(validInputs);
FragmentSignupBinding binding = FragmentSignupBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onDestroy() {
super.onDestroy();
this.disposables.dispose();
}
}
之前对此进行了编辑,以关注我认为的问题所在。这是现在的全貌。
我认为每个事件类型只能有 1 个可观察对象。
像您的情况一样添加第二个会删除前一个。
尝试注释掉除一个 RxTextView.textChanges(txtEmail)
之外的所有内容。
查看关于这个问题的讨论:only one observable at a time?
我没有正确处理片段数据绑定。在代码的这一点上,它甚至没有必要。我没有意识到设置会影响 RxBindingings。
以下代码已损坏...
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signup, container, false);
...
FragmentSignupBinding binding = FragmentSignupBinding.inflate(inflater, container, false);
return binding.getRoot();
}
以下代码现在有效....
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentSignupBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_signup, container, false);
View view = binding.getRoot();
ButterKnife.bind(this, view);
this.disposables = new CompositeDisposable();
....
return view;
}
RxTextView.textChanges(txtEmail)
.subscribe(new Observer<CharSequence>() {
这是错误的,应该是
disposables.add(RxTextView.textChanges(txtEmail)
.subscribe(new DisposableObserver<CharSequence>() {
此外,例如 RxView.clicks(
只能支持 1 个侦听器,因为它包装了 setOnClickListener()
方法。
所以您的代码很有可能会像这样工作
Observable<CharSequence> obsName;
...
public void onCreate(...) {
...
obsName = RxTextView.textChanges(txtName).share();
然后在任何地方使用 obsName
,对于其他 Rx___
调用也是如此。
试图找出为什么 RxTextView.textChanges 只发出初始值。我的理解是它应该向下游订阅发送所有新的文本更改。
这是代码
public class SignupFragment extends Fragment {
@BindView(R.id.text_name) EditText txtName;
@BindView(R.id.text_email) EditText txtEmail;
@BindView(R.id.text_password) EditText txtPassword;
@BindView(R.id.text_confirm_password) EditText txtConfirmPassword;
@BindView(R.id.btn_signup) Button btnSignup;
SignupViewModel viewModel;
private CompositeDisposable disposables;
FragmentSignupBinding binding;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signup, container, false);
ButterKnife.bind(this, view);
this.disposables = new CompositeDisposable();
RxTextView.textChanges(txtEmail)
.subscribe(new Observer<CharSequence>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(CharSequence charSequence) {
Log.d("Subscription", charSequence.toString());
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
Log.d("Subscription", "On Complete");
}
});
viewModel = ViewModelProviders.of(this).get(SignupViewModel.class);
viewModel.applyInputs(RxTextView.textChanges(txtName),
RxTextView.textChanges(txtEmail),
RxTextView.textChanges(txtPassword),
RxTextView.textChanges(txtConfirmPassword),
RxView.clicks(btnSignup));
Disposable validInputs =
viewModel.validInputs()
.doOnNext(new Consumer<Boolean>() {
@SuppressLint("TimberArgCount")
@Override
public void accept(Boolean aBoolean) throws Exception {
Timber.d("Valid inputs: %b", aBoolean.booleanValue());
}
})
.subscribe(RxView.visibility(btnSignup));
disposables.add(validInputs);
FragmentSignupBinding binding = FragmentSignupBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onDestroy() {
super.onDestroy();
this.disposables.dispose();
}
}
之前对此进行了编辑,以关注我认为的问题所在。这是现在的全貌。
我认为每个事件类型只能有 1 个可观察对象。
像您的情况一样添加第二个会删除前一个。
尝试注释掉除一个 RxTextView.textChanges(txtEmail)
之外的所有内容。
查看关于这个问题的讨论:only one observable at a time?
我没有正确处理片段数据绑定。在代码的这一点上,它甚至没有必要。我没有意识到设置会影响 RxBindingings。
以下代码已损坏...
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signup, container, false);
...
FragmentSignupBinding binding = FragmentSignupBinding.inflate(inflater, container, false);
return binding.getRoot();
}
以下代码现在有效....
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentSignupBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_signup, container, false);
View view = binding.getRoot();
ButterKnife.bind(this, view);
this.disposables = new CompositeDisposable();
....
return view;
}
RxTextView.textChanges(txtEmail)
.subscribe(new Observer<CharSequence>() {
这是错误的,应该是
disposables.add(RxTextView.textChanges(txtEmail)
.subscribe(new DisposableObserver<CharSequence>() {
此外,例如 RxView.clicks(
只能支持 1 个侦听器,因为它包装了 setOnClickListener()
方法。
所以您的代码很有可能会像这样工作
Observable<CharSequence> obsName;
...
public void onCreate(...) {
...
obsName = RxTextView.textChanges(txtName).share();
然后在任何地方使用 obsName
,对于其他 Rx___
调用也是如此。