Created
November 18, 2015 04:46
-
-
Save ShinjiKobayashi/65fa5c0b4935ef056556 to your computer and use it in GitHub Desktop.
DialogFragmentのベースクラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DialogFragmentBase extends DialogFragment { | |
public interface OnDialogActionListerner { | |
void onPositiveButtonClicked(); | |
void onNegativeButtonClicked(); | |
} | |
public static class Builder { | |
// ActivityかFragmentどちらかは必ずInputさせる | |
final AppCompatActivity mActivity; | |
final Fragment mFragment; | |
public static final String BUNDLE_KEY_TITLE = "title"; | |
public static final String BUNDLE_KEY_MESSAGE = "message"; | |
public static final String BUNDLE_KEY_POSITIVE_LABEL = "positive_label"; | |
public static final String BUNDLE_KEY_NEGATIVE_LABEL = "negative_label"; | |
private String mTitle; | |
private String mMessage; | |
private String mPositiveButtonLabel; | |
private String mNeagtiveButtonLabel; | |
public <A extends AppCompatActivity & OnDialogActionListerner> Builder(AppCompatActivity activity) { | |
mActivity = activity; | |
mFragment = null; | |
} | |
public <F extends Fragment & OnDialogActionListerner> Builder(F fragment) { | |
mActivity = null; | |
mFragment = fragment; | |
} | |
public Builder setTitle(String title) { | |
mTitle = title; | |
return this; | |
} | |
// 他のケース作ってないけど、同じ形式でResからの入力サポートできる | |
public Builder setTitle(int resId) { | |
return setTitle(getContext().getResources().getString(resId)); | |
} | |
public Builder setMessage(String message) { | |
mMessage = message; | |
return this; | |
} | |
public Builder setPositiveButtonLabel(String positiveButtonLabel) { | |
mPositiveButtonLabel = positiveButtonLabel; | |
return this; | |
} | |
public Builder setNeagtiveButtonLabel(String neagtiveButtonLabel) { | |
mNeagtiveButtonLabel = neagtiveButtonLabel; | |
return this; | |
} | |
public DialogFragmentBase build(){ | |
if(mTitle == null){ | |
return null; | |
} | |
Bundle bundle = new Bundle(); | |
bundle.putString(BUNDLE_KEY_TITLE, mTitle); | |
bundle.putString(BUNDLE_KEY_MESSAGE, mMessage); | |
bundle.putString(BUNDLE_KEY_POSITIVE_LABEL, mPositiveButtonLabel); | |
bundle.putString(BUNDLE_KEY_NEGATIVE_LABEL, mNeagtiveButtonLabel); | |
DialogFragmentBase fragment = new DialogFragmentBase(); | |
fragment.setArguments(bundle); | |
return fragment; | |
} | |
private Context getContext() { | |
return (mActivity != null ? mActivity : mFragment.getActivity()) | |
.getApplicationContext(); | |
} | |
} | |
private OnDialogActionListerner mListener; | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
if(getActivity() instanceof OnDialogActionListerner){ | |
mListener = (OnDialogActionListerner)getActivity(); | |
}else if(getParentFragment() instanceof OnDialogActionListerner){ | |
mListener = (OnDialogActionListerner)getParentFragment(); | |
} | |
} | |
@Override | |
public void onDetach() { | |
mListener = null; | |
super.onDetach(); | |
} | |
@NonNull | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
Bundle bundle = getArguments(); | |
// DialogのLayoutを変更したいのであれば、ここでinflateする | |
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()); | |
if(bundle.getString(Builder.BUNDLE_KEY_TITLE) != null){ | |
dialogBuilder.setTitle(bundle.getString(Builder.BUNDLE_KEY_TITLE)); | |
} | |
if(bundle.getString(Builder.BUNDLE_KEY_MESSAGE) != null){ | |
dialogBuilder.setMessage(bundle.getString(Builder.BUNDLE_KEY_MESSAGE)); | |
} | |
if(bundle.getString(Builder.BUNDLE_KEY_POSITIVE_LABEL) != null){ | |
dialogBuilder.setPositiveButton(bundle.getString(Builder.BUNDLE_KEY_POSITIVE_LABEL), | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}); | |
} | |
if(bundle.getString(Builder.BUNDLE_KEY_NEGATIVE_LABEL) != null){ | |
dialogBuilder.setNegativeButton(bundle.getString(Builder.BUNDLE_KEY_NEGATIVE_LABEL), | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}); | |
} | |
return dialogBuilder.create(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment