如何在解除另一个DialogFragment后刷新DialogFragment

时间:2017-04-14 22:19:23

标签: android dialog dialogfragment

我有一个AppCompatActivity,在某些时候显示DialogFragment。在此对话框中,有些项目在删除之前我会要求确认。通过另一个是/否DialogFragment询问该确认。当用户在第二个对话框中单击“是”时,我希望第一个对话框刷新其ListView(只需要更新适配器并调用其notifyDataSetChanged方法)。问题是我不知道何时更新列表视图。

因为从各种来源调用了删除功能,所以我在活动级别实现了一个监听器接口并调用了" onDeleteRequest"每当我需要删除项目时,来自该界面的事件,以及打开确认对话框并执行实际删除的活动。

由于我不太关心在不必要的情况下刷新ListView,我尝试更新onResume事件中的列表,但是当我回到第一个对话框后没有调用该事件确认一被驳回。

所以我的问题是:我怎么知道对话框A顶部显示的对话框B何时被解除,所以我可以相应刷新对话框A?

编辑:支持我的问题的一些代码:

我的活动课程:

public class MonthActivity
        extends AppCompatActivity
        implements OnEditCalendarsDialogListener
{
    ...

    //That's where dialog A is shown
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        ...

        if (id == R.id.action_select_calendar) {
            final CalendarSelection currentSelection = mCalendarSelectionAdapter.getCurrentCalendarSelection();

            if (currentSelection != null) {
                EditCalendarsDialogFragment dialogFragment = EditCalendarsDialogFragment.newInstance(currentSelection);
                dialogFragment.show(getSupportFragmentManager());
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    ...

    //OnEditCalendarsDialogListener interface implementation

    //That's where Dialog B is shown over Dialog A
    @Override
    public void onEditCalendarsDialogDelete(long calendarID) {
        final Repository repository = Repository.getInstance(this);
        final Calendar calendar = repository.fetchOneByID(Calendar.class, calendarID);

        if (calendar != null) {
            YesNoDialog yesNoDialog = YesNoDialog.newInstance(this, R.string.yes_no_dialog_confirmation, R.string.yes_no_dialog_calendar_delete);
            setCurrentOnDecisionClickListener(new OnPositiveClickListener() {
                @Override
                public boolean onPositiveClick(DialogInterface dialog) {
                    //Delete calendar
                    repository.delete(calendar);
                    //That's where I'd like to notify Dialog A that it needs to be refreshed
                    return true;
                }
            });
            yesNoDialog.show(getSupportFragmentManager());
        }
    }
}

我的对话类

public class EditCalendarsDialogFragment
        extends DialogFragment
{
    private OnEditCalendarsDialogListener mDialogListener;

    public static EditCalendarsDialogFragment newInstance(CalendarSelection calendarSelection) {
        EditCalendarsDialogFragment dialog = new EditCalendarsDialogFragment();
        Bundle arguments = new Bundle();

        if (calendarSelection != null) {
            arguments.putLong(KEY_ID, calendarSelection.getID());
        }
        else {
            arguments.putLong(KEY_ID, 0L);
        }

        dialog.setArguments(arguments);

        return dialog;
    }

    ...

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mDialogListener = (OnEditCalendarsDialogListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnCalendarSelectionDialogListener");
        }
    }

    ...

    private View getLayoutView() {
        View rootView = getActivity().getLayoutInflater().inflate(R.layout.calendar_list, null, false);

        if (rootView != null) {
            mCalendars = (ListView) rootView.findViewById(R.id.calendars);
            if (mCalendars != null) {
                //Create adaptor
                mCalendarAdapter = new ArrayAdapter<Calendar>(
                        getContext(),
                        android.R.layout.simple_list_item_2,
                        android.R.id.text1,
                        new ArrayList<Calendar>()
                ) {
                    @NonNull
                    @Override
                    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                        View view = super.getView(position, convertView, parent);

                        final Calendar calendar = getItem(position);

                        if (calendar != null && calendar.hasID()) {
                            ...
                            view.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {
                                    if (mDialogListener != null) {
                                        //That's where I request delete from calling activity
                                        mDialogListener.onEditCalendarsDialogDelete(calendar.getID());
                                    }
                                    return true;
                                }
                            });
                        }

                        return view;
                    }
                };
                mCalendars.setAdapter(mCalendarAdapter);
                refreshCalendarList();
            }
        }

        return rootView;
    }

}

2 个答案:

答案 0 :(得分:1)

使用EventBus

注册对话框A以收听事件。当您关闭对话框B发布事件并传递listitem的适配器位置或您要使用的任何数据来识别要删除的项目时。在对话框内部写一个函数来接收你删除项目的事件。

答案 1 :(得分:0)

好的,所以我最终使用了“过度滥用 - 回调”方法。

我创建了以下界面:

public interface OnDeletedListener {
    void onDeleted();
}

更新了OnEditCalendarsDialogListener接口,以便回调也具有对此接口的回调:

public interface OnEditCalendarsDialogListener {
    void onEditCalendarsDialogDelete(long calendarID, OnDeletedListener onDeletedListener);
}

在“Dialog A”类中实现了OnDeletedListener接口:

public class EditCalendarsDialogFragment
        extends DialogFragment
        implements OnDeletedListener
{
    ...

    //OnDeletedListener interface implementation

    @Override
    public void onDeleted() {
        //That's where I'm called back after item is deleted
        refreshCalendarList();
    }

    ...

    private View getLayoutView() {
        ...
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mDialogListener != null) {
                    //That's where I request delete from calling activity, asking to call me back once deleted
                    mDialogListener.onEditCalendarsDialogDelete(calendar.getID(), EditCalendarsDialogFragment.this);
                }
                return true;
            }
        });
        ...
    }
}

最后,在接受并执行删除时调用回调:

public class MonthActivity
        extends AppCompatActivity
        implements OnEditCalendarsDialogListener
{
    //OnEditCalendarsDialogListener interface implementation

    //That's where Dialog B is shown over Dialog A
    @Override
    public void onEditCalendarsDialogDelete(long calendarID, final OnDeletedListener onDeletedListener) {
        final Repository repository = Repository.getInstance(this);
        final Calendar calendar = repository.fetchOneByID(Calendar.class, calendarID);

        if (calendar != null) {
            YesNoDialog yesNoDialog = YesNoDialog.newInstance(this, R.string.yes_no_dialog_confirmation, R.string.yes_no_dialog_calendar_delete);
            setCurrentOnDecisionClickListener(new OnPositiveClickListener() {
                @Override
                public boolean onPositiveClick(DialogInterface dialog) {
                    //Delete calendar
                    repository.delete(calendar);
                    //That's where I notify Dialog A that it needs to be refreshed
                    if (onDeletedListener != null) {
                        onDeletedListener.onDeleted();
                    }
                    return true;
                }
            });
            yesNoDialog.show(getSupportFragmentManager());
        }
    }
}

顺利运作!