8
8
import android .support .v4 .app .DialogFragment ;
9
9
import android .support .v4 .app .Fragment ;
10
10
import android .support .v4 .app .FragmentActivity ;
11
+ import android .support .v4 .app .FragmentManager ;
11
12
import android .support .v4 .util .Pair ;
12
13
import android .support .v7 .app .AlertDialog ;
13
14
import android .text .TextUtils ;
14
15
15
16
/**
16
- * Created by Julien Arzul on 09/11/2016.
17
+ * Simple implementation of a {@link DialogFragment} that allows you to specify the content of the {@link AlertDialog}
18
+ * displayed without subclassing it.
19
+ * <p>
20
+ * This DialogFragment must be instantiated with a {@link SimpleDialogContent} via the factory method {@link
21
+ * #newInstance(SimpleDialogContent)}
22
+ * <p>
23
+ * You can easily define listeners for the buttons set in the {@link SimpleDialogContent} with one of the two methods:
24
+ * <ul>
25
+ * <li>the enclosing activity must implement one or more of the listener interfaces</li>
26
+ * <li>the enclosing fragment must be set as the targetFragment of the {@link SimpleDialogFragment} and implement one
27
+ * or more of the listener interfaces</li>
28
+ * </ul>
29
+ * <p>
30
+ * There are three different listener interfaces that you can implement:
31
+ * <ul>
32
+ * <li>{@link OnPositiveButtonClickListener}</li>
33
+ * <li>{@link OnNegativeButtonClickListener}</li>
34
+ * <li>{@link OnNeutralButtonClickListener}</li>
35
+ * </ul>
36
+ * If the user chooses to listen to the click on the dialog's button, it is recommended to add a request code to the
37
+ * {@link SimpleDialogContent}. That request code will be given to the onClick method and will allow the user to
38
+ * determine which {@link SimpleDialogFragment} invoked the method. It is especially useful when a single {@link
39
+ * android.app.Activity} or {@link Fragment} needs to display several {@link SimpleDialogFragment}
40
+ * <p>
17
41
* Copyright @ Julien Arzul 2016
18
42
*/
19
43
public class SimpleDialogFragment extends DialogFragment implements DialogInterface .OnClickListener {
@@ -26,6 +50,16 @@ public class SimpleDialogFragment extends DialogFragment implements DialogInterf
26
50
27
51
private SimpleDialogContent dialogContent = null ;
28
52
53
+ /**
54
+ * Creates a new instance of a {@link SimpleDialogFragment}. It must be given a {@link SimpleDialogContent} to
55
+ * define the {@link AlertDialog} content.
56
+ *
57
+ * @param dialogContent Instance of {@link SimpleDialogContent} representing the content of the {@link AlertDialog}
58
+ * that will be displayed.
59
+ *
60
+ * @return A new instance of {@link SimpleDialogFragment}. Don't forget to call {{@link #show(FragmentManager,
61
+ * String)}} to actually display the {@link DialogFragment}
62
+ */
29
63
public static SimpleDialogFragment newInstance (SimpleDialogContent dialogContent ) {
30
64
Bundle args = new Bundle ();
31
65
@@ -128,18 +162,48 @@ private <T> Pair<T, Integer> getListener(Class<T> listenerClazz) {
128
162
return null ;
129
163
}
130
164
165
+ /**
166
+ * Interface that must be implemented by the enclosing activity (or target fragment) in order to listen to click on
167
+ * the dialog positive's button.
168
+ */
131
169
public interface OnPositiveButtonClickListener {
132
170
171
+ /**
172
+ * This method will be invoked when the positive button of the dialog is clicked.
173
+ *
174
+ * @param dialog The dialog that received the click.
175
+ * @param requestCode The request code used to show the {@link SimpleDialogFragment}
176
+ */
133
177
void onDialogPositiveButtonClicked (DialogInterface dialog , Integer requestCode );
134
178
}
135
179
180
+ /**
181
+ * Interface that must be implemented by the enclosing activity (or target fragment) in order to listen to click on
182
+ * the dialog negative's button.
183
+ */
136
184
public interface OnNegativeButtonClickListener {
137
185
186
+ /**
187
+ * This method will be invoked when the negative button of the dialog is clicked.
188
+ *
189
+ * @param dialog The dialog that received the click.
190
+ * @param requestCode The request code used to show the {@link SimpleDialogFragment}
191
+ */
138
192
void onDialogNegativeButtonClicked (DialogInterface dialog , Integer requestCode );
139
193
}
140
194
195
+ /**
196
+ * Interface that must be implemented by the enclosing activity (or target fragment) in order to listen to click on
197
+ * the dialog neutral's button.
198
+ */
141
199
public interface OnNeutralButtonClickListener {
142
200
201
+ /**
202
+ * This method will be invoked when the neutral button of the dialog is clicked.
203
+ *
204
+ * @param dialog The dialog that received the click.
205
+ * @param requestCode The request code used to show the {@link SimpleDialogFragment}
206
+ */
143
207
void onDialogNeutralButtonClicked (DialogInterface dialog , Integer requestCode );
144
208
}
145
209
}
0 commit comments