我们如何在RxJava
链中链接对话框片段并等待其结果(单击该按钮)。
类似的东西:
buttonClickObservable.flatMap(v -> showDialogAndWaitForResponse()).subscribe(dialogResult -> doSomething());
这甚至可能吗?有人可以在这里指导我。
谢谢。
答案 0 :(得分:0)
如果你正在使用RxBinding
库,你可以这样做:
buttonClickObservable
.doOnNext(e -> showDialog())
.flatMap(v -> dialogBtnClickObservable)
.subscribe(clicked -> Log.d("TAG", "It's working!"));
编辑:好的,事实证明使用RxBinding观察对话框的视图并不容易。因此,这是使用RxBinding
和PublishSubject
public class MainActivity extends AppCompatActivity {
Button btn;
//here you can declare the Subject with the type
//you're expecting as a result from the dialog
public static final Subject<String> fragmentResultSubject = PublishSubject.create();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
RxView.clicks(btn)
.doOnNext(e -> showDialog())
.flatMap(v -> fragmentResultSubject)
.subscribe(fragmentResult -> Log.d("TAG", fragmentResult));
}
private MyDialogFragment showDialog() {
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getSupportFragmentManager(), "FRAG_TAG");
return myDialogFragment;
}
public static class MyDialogFragment extends DialogFragment {
Button dialogBtn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dialog, container, false);
dialogBtn = rootView.findViewById(R.id.dialogBtn);
dialogBtn.setOnClickListener(v -> fragmentResultSubject.onNext("Fragment btn clicked"));
return rootView;
}
}
}
答案 1 :(得分:0)
我将使用以下方法:
SimpleActivity
{
"_id": {
"$oid": "5cbd9009740fb12e08b5592f"
},
"item": [
"ABC"
"DEF"
],
"res_name": "XYZ"
"price": [
"150",
"450"
],
"category": [
"Starters",
"Main course"
]
}
SimpleDialogFragment
public class SimpleActivity extends AppCompatActivity {
private SimpleDialogViewModel dialogViewModel;
private CompositeDisposable compositeDisposable;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialogViewModel = ViewModelProviders.of(this).get(SimpleDialogViewModel.class);
compositeDisposable = new CompositeDisposable();
showDialog();
}
@Override
protected void onResume() {
super.onResume();
Disposable disposable =
dialogViewModel
.actionStream()
.subscribe(
result -> {
if (AlertDialog.BUTTON_POSITIVE == result) {
// User clicked yes
}
if (AlertDialog.BUTTON_NEGATIVE == result) {
// User clicked no
}
}
);
compositeDisposable.add(disposable);
}
@Override
protected void onPause() {
super.onPause();
compositeDisposable.clear();
}
private void showDialog() {
SimpleDialogFragment dialogFragment = new SimpleDialogFragment();
dialogFragment.show(getSupportFragmentManager(), SimpleDialogFragment.TAG);
}
}
SimpleDialogViewModel
public class SimpleDialogFragment extends DialogFragment {
public static final String TAG = "SimpleDialogFragment";
private SimpleDialogViewModel dialogViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialogViewModel = ViewModelProviders.of(getActivity()).get(SimpleDialogViewModel.class);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_simple_message, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
final View btnYes = view.findViewById(R.id.yes);
final View btnNo = view.findViewById(R.id.no);
btnYes.setOnClickListener(v -> dialogViewModel.onClickYes());
btnNo.setOnClickListener(v -> dialogViewModel.onClickNo());
}
}