且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有MVVM的Android自定义对话框

更新时间:2023-01-23 21:46:24

尝试此解决方案.它为我工作.

Try this solution. It worked for me.

从调用对话框的位置创建活动字段,并将其传递给生命周期所有者

Create a field of your activity from where you are calling the dialog and pass this in place of lifecycleowner

public class YourDialog extends DialogFragment {

private YourActivity activity;

    public static YourDialog newInstance(YourActivity activity) {
        YourDialog dialog = new YourDialog();
        dialog.activity = activity;
        return dialog;
    }

    private void observeViewModel(ProjectListViewModel viewModel) {
    // Update the list when the data changes
    viewModel.getProjectListObservable().observe( activity , new Observer<List<Project>>() {
        @Override
        public void onChanged(@Nullable List<Project> projects) {
            if (projects != null) {
                //…
                projectAdapter.setProjectList(projects);
            }
        }
    });

}

如果需要,可以在此处引用mvvm的示例.

You can refer the example of mvvm here if you want