我有一个包含片段列表的FragmentActivity(具有在它们之间导航的方法)。
在这些片段中的一个中,我需要调用一个DialogFragment来显示该片段中包含的图片的“缩放”。
但似乎无法直接从Fragment调用DialogFragment。
是否有任何方法可以获得某种“回调”到FragmentActivity,以便使其在片段上显示DialogFragment?
或者只是通过Fragment直接调用它的某个“故障”。
如果是这样,我的选项有哪些?
我有一个包含片段列表的FragmentActivity(具有在它们之间导航的方法)。
在这些片段中的一个中,我需要调用一个DialogFragment来显示该片段中包含的图片的“缩放”。
但似乎无法直接从Fragment调用DialogFragment。
是否有任何方法可以获得某种“回调”到FragmentActivity,以便使其在片段上显示DialogFragment?
或者只是通过Fragment直接调用它的某个“故障”。
如果是这样,我的选项有哪些?
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;
}
}
Activity
;) - Manitobainsantiate(Context, String)
已被弃用。 - wbk727如果您需要在片段内显示一个片段对话框,这将有所帮助。
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");
检查导入语句。 如果我们使用
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;
我遇到了同样的问题,通过导入解决了
import android.support.v4.app.ListFragment;
替代
import android.app.ListFragment;
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);
对我而言,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.");
}
}
onCreateView()
иҖҢйқһonCreateDialog()
гҖӮ - EpicPandaForceFragmentActivity
中,您可以通过在onAttach()
中附加到Fragment
的接口来调用它。请参见链接的答案,因为那里更详细,我只是不想重复所有内容并复制粘贴,所以我只留下了重要的元素。 - EpicPandaForce 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() {
...
}
}
你可以像下面展示的那样从碎片中调用:
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");
}
你的调用片段
public class Order_Today extends Fragment {
public void callDialogFragment() {
DialogFragmentToOpen add = new DialogFragmentToOpen();
add.show(getActivity().getSupportFragmentManager(),"dialog");
}
}
public class DialogFragmentToOpen extends DialogFragment {}
ShowValueDialog().showNow(
this@OrderFragment.childFragmentManager,
"your string"
)
**or You Can use this**
this@OrderFragment.parentFragmentManager?.let { it1 -> ShowValueDialog().showNow(it1,"your string") }