Android фрагмент внутри диалога
У меня есть проблема, где мне нужно показать fragment
в android.app.Dialog
Вот xml-код
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/marchecharts"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
Я хочу заменить marchecharts
своим фрагментом, может кто-нибудь помочь
Спасибо
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.marche_charts_parent);
//this is the part I think I need
Fragment fragment = new MarcheChartsFragment();
FragmentTransaction ft = ((FragmentActivity) dialog.getOwnerActivity()).getFragmentManager().beginTransaction();
ft.replace(R.id.marchecharts, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
dialog.show();
1 ответ:
Обычно вы напрямую используетеDialogFragment , имя которого объясняется самостоятельно.
Вот пример моего кода с
Таким образом, в основном вы создаетеint
send as arg.DialogFragment
, который расширяетDialogFragment
. Вы должны написать методыnewInstance
иonCreateDialog
. Затем вы создаете новый экземпляр этого фрагмента в вызывающем фрагменте.public class YourDialogFragment extends DialogFragment { public static YourDialogFragment newInstance(int myIndex) { YourDialogFragment yourDialogFragment = new YourDialogFragment(); //example of passing args Bundle args = new Bundle(); args.putInt("anIntToSend", myIndex); yourDialogFragment.setArguments(args); return yourDialogFragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //read the int from args int myInteger = getArguments().getInt("anIntToSend"); View view = inflater.inflate(R.layout.your_layout, null); //here read the different parts of your layout i.e : //tv = (TextView) view.findViewById(R.id.yourTextView); //tv.setText("some text") return view; } }
Вызов фрагмента диалога выполняется из другого фрагмента, делая это. Обратите внимание, что значение
0
является int I посылать.YourDialogFragment yourDialogFragment = YourDialogFragment.newInstance(0); YourDialogFragment.show(getFragmentManager().beginTransaction(), "DialogFragment");
В вашем случае, если вам не нужно ничего передавать, удалите соответствующие строки в DialogFragment и не передавайте значения в
YourDialogFragment.newInstance()
РЕДАКТИРОВАТЬ / СЛЕДОВАТЬ
Не уверен, что действительно понимаю ваш вопрос. Если вам просто нужно заменить фрагмент другим, вы используете
getFragmentManager().beginTransaction().replace(R.id.your_fragment_container, new YourFragment()).commit();