Use of several DialogFragment
-
I've got a few boxes in my program that have been filled with fragments. Two of the tabs, different data, two different DialogFragmenta, and each of the dialogues uses an interface by which I transmit the necessary data to the main activation. For example, one of the interfaces
public interface MyDialogListener { void onDialogPositiveClick(DialogFragment dialogFragment, int value); void onDialogNegativeClick(DialogFragment dialogFragment); }
And then from the main activate, I'm transferring data to another fragment. Just using this interface.
implements MyDialogFragment.MyDialogListener
and in the interface, I write the necessary data for another data fragment.@Override public void onDialogPositiveClick(DialogFragment dialogFragment, int value) { // Код для передачи данных в другой фрагмент TimerFragment timerFragment = (TimerFragment) mAdapter.getItem(1); timerFragment.setStartTime(value); }
@Override
public void onDialogNegativeClick(DialogFragment dialogFragment) {
}
I mean, every tab is a fragment that can cause DialogFragment and put some weight in that pelvis. DialogFragment sequencing fer MineActivity stat OtherFragment.
I've completely figured it out. But the next question arose. Other tabes (other fragment) need to introduce other data and the same dialogue will not be possible. To that end, I created a new dialogue, but how am I supposed to transfer the data to the second fragment of another tab? Can one interface be used for both dialogues? For one fragment, I need only one numerical meaning, and for another fragment, I need to pass two more strings. Can you tell me how this can be implemented? I'm guessing we need to take the interface into a separate file, but I don't know how to do it.
Just in case, this is the code of one dialogue:public class MyDialogFragment extends DialogFragment {
private int mMin;
private int mSec;
private int mValue = mMin + mSec;public interface MyDialogListener {
void onDialogPositiveClick(DialogFragment dialogFragment, int value);
void onDialogNegativeClick(DialogFragment dialogFragment);
}private MyDialogListener mListener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {// Создаем объект класса AlertDialog.Builder и далее с помощью этого объекта строим наше диалоговое окно AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Создаем объект абстраткного класса LayoutInflater, чтобы с его помощью передать свой макет в AlertDialog через метод setView() LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_timer, null); NumberPicker numberPickerMin = (NumberPicker) view.findViewById(R.id.numberPickerMin); final NumberPicker numberPickerSec = (NumberPicker) view.findViewById(R.id.numberPickerSec); numberPickerMin.setMaxValue(60); numberPickerMin.setMinValue(0); numberPickerMin.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mMin = newVal * 60; } }); numberPickerSec.setMaxValue(60); numberPickerSec.setMinValue(0); numberPickerSec.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mSec = newVal; } }); builder.setTitle(R.string.set_time) .setView(view) .setPositiveButton(R.string.set, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mListener.onDialogPositiveClick(MyDialogFragment.this, mValue); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mListener.onDialogNegativeClick(MyDialogFragment.this); } }); return builder.create();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);try { mListener = (MyDialogListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); }
}
♪
-
For the use of one intégé in different classes, it is not necessary to separate class. You can use it in another class like:
ClassThatHoldInterfaceDefenition.InterFaceName = ...; //в вашем случае //MyDialogFragment.MyDialogListener = (MyDialogFragment.MyDialogListener)activity;
For the transmission of additional data, you can add the method to the interface.
void onDialogPositiveClick(DialogFragment dialogFragment, int value, String str1, String str2);
or make one argument
Bundle
And you're beating everything you want in any number or combination.