可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Here's my problem :
I do have a FragmentActivity wich contains a Fragment list (with methods to navigate between them)
In one of thoose Fragments i need to call a DialogFragment to display a "zoom" on a Picture contained in that fragment.
But it seems that you can't call a DialogFragment directly from a Fragment.
Is there any way to get somekind of "callback" to the FragmentActivity to make this display the DialogFragment over the fragment.
Or simply a "glitch" to call it directly from the Fragment.
If it's the case do you guyz knows a good tutorial about it ?
Best regards,
Elie Page
回答1:
When you create a new Dialog
, you can simply call it using this (very) simple method from a Fragment
.
DialogFragment dialog = DialogFragment.instantiate(getActivity(), "Hello world");
dialog.show(getFragmentManager(), "dialog");
If you want to use your own dialog, please use that kind of code.
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;
}
}
回答2:
Check for import statements.
If we use
ExampleDialogFragment dialog = new ExampleDialogFragment ();
dialog .show(getFragmentManager(), "example");
Then make sure to import
import android.app.DialogFragment;
import android.app.Fragment;
not from the support library.
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
回答3:
it will help if you need to show a fragment dialog inside a fragment
Dialogfragment
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;
}
}
now showing the fragment dialog into a fragment
DialogFragment dialogFragment = new DialogFragment ();
dialogFragment.show(getActivity().getFragmentManager(),"simple dialog");
回答4:
For me, it was the following: https://stackoverflow.com/a/25056160/2413303
The most important parts are that you need to have a Callback
for your dialog fragment:
public class MyFragment extends Fragment implements MyDialog.Callback
Which kinda works like this
public class MyDialog extends DialogFragment implements View.OnClickListener {
public static interface Callback
{
public void accept();
public void decline();
public void cancel();
}
You make the Activity show the dialog for you from the Fragment:
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(this, 1); //request code
activity_showDialog.showDialog(dialog);
Where showDialog()
for me was the following method:
@Override
public void showDialog(DialogFragment dialogFragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
dialogFragment.show(fragmentManager, "dialog");
}
And you call back onto your target fragment:
@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.");
}
}
回答5:
Try this simple class I did in a myown project:
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() {
...
}
}
and you can call from a Fragment as shown below:
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");
}
回答6:
I had same issue
solved by import
import android.support.v4.app.ListFragment;
instead of
import android.app.ListFragment;