我的问题是这样的:我确实有一个FragmentActivity,它包含一个Fragment列表(有在它们之间导航的方法),在其中一个Fragment中,我需要调用一个DialogFragment来显示一个 "缩放 "在该Fragment中包含的图片。
但似乎你不能直接从一个Fragment中调用一个DialogFragment。
有没有什么方法可以让FragmentActivity得到某种形式的 "回调",使其在片段上显示DialogFragment。
或者干脆来个 "小插曲",直接从Fragment中调用它。
如果是这样的话,你们知道有什么好的教程吗?
最好的问候。
Elie Page
当你创建一个新的 Dialog
,你可以简单地使用这个(非常)简单的方法从一个 Fragment
.
DialogFragment dialog = DialogFragment.instantiate(getActivity(), "Hello world");
dialog.show(getFragmentManager(), "dialog");
如果你想使用自己的对话框,请使用那种代码。
public class MyDialogFragment extends DialogFragment
{
//private View pic;
public MyDialogFragment()
{
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_my_dialog, new LinearLayout(getActivity()), false);
// Retrieve layout elements
TextView title = (TextView) view.findViewById(R.id.text_title);
// Set values
title.setText("Not perfect yet");
// Build dialog
Dialog builder = new Dialog(getActivity());
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setContentView(view);
return builder;
}
}
检查导入语句.如果我们使用
ExampleDialogFragment dialog = new ExampleDialogFragment ();
dialog .show(getFragmentManager(), "example");
然后确保导入
import android.app.DialogFragment;
import android.app.Fragment;
不是来自支持库。
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
如果您需要在一个片段中显示一个片段对话框,它会有所帮助。
public class DialogBoxFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
getDialog().setTitle("simple dialog");
return rootView;
}
}
DialogFragment dialogFragment = new DialogFragment ();
dialogFragment.show(getActivity().getFragmentManager(),"simple dialog");
对我来说,是以下几点。https:/stackoverflow.coma250561602413303。
最重要的部分是,你需要有一个。Callback
的对话片段。
public class MyFragment extends Fragment implements MyDialog.Callback
它的工作原理是这样的
public class MyDialog extends DialogFragment implements View.OnClickListener {
public static interface Callback
{
public void accept();
public void decline();
public void cancel();
}
你让活动从片段中为你显示对话框。
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(this, 1); //request code
activity_showDialog.showDialog(dialog);
Where showDialog()
对我来说是下面的方法。
@Override
public void showDialog(DialogFragment dialogFragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
dialogFragment.show(fragmentManager, "dialog");
}
然后你回调到你的目标片段上。
@Override
public void onClick(View v)
{
Callback callback = null;
try
{
callback = (Callback) getTargetFragment();
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
throw e;
}
if (callback != null)
{
if (v == acceptButton)
{
callback.accept();
this.dismiss();
}
else if (...) {...}
}
else
{
Log.e(this.getClass().getSimpleName(), "Callback was null.");
}
}
试试我在自己的项目中做的这个简单的类:
public class UIDialogMessage extends DialogFragment {
public static UIDialogMessage newInstance(int aTitleID, int aMessageID) {
return newInstance(aTitleID, aMessageID, true);
}
public static UIDialogMessage newInstance(int aTitleID, int aMessageID, boolean aDoIt) {
UIDialogMessage frag = new UIDialogMessage();
Bundle args = new Bundle();
args.putInt("titleID", aTitleID);
args.putInt("messageID", aMessageID);
args.putBoolean("keyBoolDoSomething", aDoIt);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int mTitleID = getArguments().getInt("titleID");
int mMessageID = getArguments().getInt("messageID");
final boolean mDoIt= getArguments().getBoolean("keyBoolDoSomething", true);
return new AlertDialog.Builder(getActivity())
.setTitle(mTitleID)
.setMessage(mMessageID)
.setPositiveButton(getResources().getString(R.string.dialog_button_gotcha),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
if (mDoIt)
doIt();
}
})
.create();
}
private void doIt() {
...
}
}
你可以从一个Fragment调用,如下图所示。
showDialog(R.string.dialog_title, R.string.dialog_message, false);
private void showDialog(int aTitleID, int aMessageID, boolean aDoIt) {
DialogFragment uiDialogMessage = UIDialogMessage.newInstance(aTitleID, aMessageID, aDoIt);
uiDialogMessage.show(getFragmentManager(), "dialog");
}
我也有同样的问题,通过导入
import android.support.v4.app.ListFragment;
而不是
import android.app.ListFragment;
您的呼叫片段
public class Order_Today extends Fragment {
public void callDialogFragment() {
DialogFragmentToOpen add = new DialogFragmentToOpen();
add.show(getActivity().getSupportFragmentManager(),"dialog");
}
}
您的对话片段
public class DialogFragmentToOpen extends DialogFragment {}