Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active December 22, 2015 10:48
Show Gist options
  • Save daichan4649/6460732 to your computer and use it in GitHub Desktop.
Save daichan4649/6460732 to your computer and use it in GitHub Desktop.
確認ダイアログ のサンプル。呼出元を FragmentType、表示文字列 で識別し、そこへ callback する。
public class ConfirmDialogFragment extends DialogFragment {
/**
* Fragment種別
*/
public enum FragmentType {
NONE(-1, ""),
/** 画面1 */
SCREEN_1(0, "login"),
/** 画面2 */
SCREEN_2(1, "confirm");
private int intValue;
private String tag;
FragmentType(int intValue, String tag) {
this.intValue = intValue;
this.tag = tag;
}
public int getIntValue() {
return intValue;
}
public String getTag() {
return tag;
}
public static FragmentType fromIntValue(int intValue) {
for (FragmentType type : FragmentType.values()) {
if (type == NONE) {
continue;
}
if (type.intValue == intValue) {
return type;
}
}
return NONE;
}
}
public interface ConfirmDialogListener {
// 呼出元の特定に使用(FragmentType, textResourceId)
/**
* OK押下時処理
* @param calledFragmentType 呼出元
* @param textResourceId 表示文字列
*/
void onConfirmOk(FragmentType calledFragmentType, int textResourceId);
/**
* キャンセル押下時処理
* @param calledFragmentType 呼出元
* @param textResourceId 表示文字列
*/
void onConfirmCancel(FragmentType calledFragmentType, int textResourceId);
}
private static final String ARGS_FRAGMENT_TYPE = "fragment_type";
private static final String ARGS_TEXT_RESOURCE = "text_resource";
/**
* インスタンス生成
* @param calledFragmentType 呼出元
* @param textResourceId 表示文字列
* @return
*/
public static DialogFragment newInstance(FragmentType calledFragmentType, int textResourceId) {
Bundle args = new Bundle();
args.putInt(ARGS_FRAGMENT_TYPE, calledFragmentType.getIntValue());
args.putInt(ARGS_TEXT_RESOURCE, textResourceId);
DialogFragment fragment = new ConfirmDialogFragment();
fragment.setArguments(args);
return fragment;
}
private FragmentType mCalledFragmentType = FragmentType.NONE;
private int mTextResourceId = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light_Dialog);
mCalledFragmentType = FragmentType.fromIntValue(getArguments().getInt(ARGS_FRAGMENT_TYPE));
mTextResourceId = getArguments().getInt(ARGS_TEXT_RESOURCE);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new Builder(getActivity());
builder.setTitle("確認");
builder.setView(createView(mTextResourceId));
builder.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
okProcess();
}
});
builder.setNegativeButton("キャンセル", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cancelProcess();
}
});
return builder.create();
}
@Override
public void onCancel(DialogInterface dialog) {
cancelProcess();
}
private View createView(int textResourceId) {
View view = getActivity().getLayoutInflater().inflate(R.layout.inflate_dialog_confirm, null, false);
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText(textResourceId);
return view;
}
private void okProcess() {
ConfirmDialogListener listener = getConfirmDialogListener();
if (listener == null) {
return;
}
listener.onConfirmOk(mCalledFragmentType, mTextResourceId);
}
private void cancelProcess() {
ConfirmDialogListener listener = getConfirmDialogListener();
if (listener == null) {
return;
}
listener.onConfirmCancel(mCalledFragmentType, mTextResourceId);
}
private ConfirmDialogListener getConfirmDialogListener() {
if (getActivity() == null) {
return null;
}
if (getActivity() instanceof ConfirmDialogListener) {
return (ConfirmDialogListener) getActivity();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment