链接RxJava中的对话框片段结果

时间:2018-02-21 09:51:13

标签: android rx-java rx-java2 android-dialogfragment dialogfragment

我们如何在RxJava链中链接对话框片段并等待其结果(单击该按钮)。

类似的东西:

buttonClickObservable.flatMap(v -> showDialogAndWaitForResponse()).subscribe(dialogResult -> doSomething());

这甚至可能吗?有人可以在这里指导我。

谢谢。

2 个答案:

答案 0 :(得分:0)

如果你正在使用RxBinding库,你可以这样做:

buttonClickObservable
            .doOnNext(e -> showDialog())
            .flatMap(v -> dialogBtnClickObservable)
            .subscribe(clicked -> Log.d("TAG", "It's working!"));

编辑:好的,事实证明使用RxBinding观察对话框的视图并不容易。因此,这是使用RxBindingPublishSubject

的完整示例
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)

我将使用以下方法:

1。组件

  • 屏幕(活动/片段)-这将显示对话框片段
  • DialogFragment-对话框。将发布有关用户操作的更新。
  • DialogViewModel-保存用户的操作流

2。实施

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