【问题标题】:How to create a Dialog fragment for Sherlock fragment?如何为 Sherlock 片段创建对话框片段?
【发布时间】:2013-08-05 14:05:58
【问题描述】:

在我的应用程序中,我使用 ActionBArSherlock 创建片段。主要活动是一个 SherlockFragmentActivity,我从中创建片段。片段位于它们自己的类文件中,它们扩展了 SherlockFragment。我需要在片段中显示警报对话框,但我无法这样做。

我用谷歌搜索了它,但徒劳无功。我检查了随 ActionBarSherlock 库提供的示例。他们展示了如何在 FragmentActivity 而不是在 Fragment 中创建对话框。我尝试在片段中实现相同但我不能使用getSupportFragmentManager()。对于 Fragment 类型,它是未定义的。

public class Fragment1 extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment2, container, false);
                    return rootView;
}

public void someFunction(){
    if(somethingHappens)
        showDialog();
}

  //the code that follows is from the sample in ActionBarSherlock

void showDialog() {
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(
            R.string.alert_dialog_two_buttons_title);
    newFragment.show(getSupportFragmentManager(), "dialog"); //getSupportFragmentManager() is undefined for the type Fragment
}

public void doPositiveClick() {
    // Do stuff here.
    Log.i("FragmentAlertDialog", "Positive click!");
}

public void doNegativeClick() {
    // Do stuff here.
    Log.i("FragmentAlertDialog", "Negative click!");
}


public static class MyAlertDialogFragment extends SherlockDialogFragment {

    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialogSupport)getActivity()).doPositiveClick(); // Cannot cast from FragmentActivity to fragment
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialogSupport)getActivity()).doNegativeClick(); //Cannot cast from FragmentActivity to fragment
                        }
                    }
                )
                .create();
    }
}

你能告诉我如何在夏洛克片段中显示对话框吗?

【问题讨论】:

  • 在片段中使用getFragmentManager

标签: android android-fragments actionbarsherlock android-alertdialog


【解决方案1】:

你可以在 SherlockFragment 中调用

getSherlockActivity().getSupportFragmentManager();

让您的片段管理器显示对话框片段。

【讨论】:

  • 哇!在 onCreateDialog 中调用 onPositiveClick 和 onNegativeClick 时会怎样?
  • 我也使用 getActivity().getAssets() 来获取资产,我应该使用 getSherlockActivty().getAssets() 吗?
  • 嗯,好的。您可以做的是将 MyAlertDialogFragment 的目标片段设置为您的 Fragment1。前任。 newFragment.setTargetFragment(this);创建对话框片段时。然后在您的 pos/neg 点击中,您可以使用 ((Fragment1)getTargetFragment()).doNegativeClick(); 检索它
  • 获得这样的资产很好。获取资产的 Sherlock 方法可能默认为获取资产的标准活动调用。
  • ((Fragment1)getTargetFragment()).doNegativeClick() 崩溃了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多