簡體   English   中英

警報對話框中的Android片段

[英]Android Fragment in alert dialog

我正在為Android應用程序搜索頁面。 我想以某種模式添加一些搜索過濾器,互聯網上的建議說我應該使用帶有片段的AlertDialog來顯示我的自定義過濾器。 我設法將片段顯示在對話框中,但是未調用與片段相關的代碼,例如onCreate。 當我在活動中直接使用該片段時,會調用其onCreate,但在AlertDialog加載該片段時則不會。 我有做錯什么嗎?還是應該以其他方式這樣做?

這是用於打開對話框的代碼

SearchFilterDialogFragment dialog = new SearchFilterDialogFragment();
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            Fragment prev = getFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                fragmentTransaction.remove(prev);
            }
            fragmentTransaction.addToBackStack(null);
            dialog.show(fragmentTransaction,"dialog");

這是對話框配置的代碼

public class SearchFilterDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.fragment_blank,null)).setTitle("Search Filters")
            .setPositiveButton(R.string.filter_dialogue_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            })
            .setNegativeButton(R.string.filter_dialogue_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });

    return builder.create();



}

這是測試片段,最終將包含實際的過濾器

public class BlankFragment extends Fragment {



public BlankFragment() {
    // Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

}

DialogFragment 片段。 它管理片段生命周期。 在BlankFragment中想要的所有內容都應該在SearchFilterDialogFragment中。 您不需要BlankFragment。

要回答有關為什么為什么未調用BlankFragment的代碼的問題,這是因為根本沒有引用或實例化BlankFragment的原因。 編碼:

builder.setView(inflater.inflate(R.layout.fragment_blank,null))

將fragment_blank布局放大到DialogFragment中,但不會啟動BlankFragment代碼。

使用自定義setView正確使用DialogFragment

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    //Set all the title, button etc. for the AlertDialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Search Filters");

    //Get LayoutInflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    //Inflate the layout but ALSO store the returned view to allow us to call findViewById
    View view = inflater.inflate(R.layout.fragment_blank,null);

    //Do all the initial code regarding the view, as you would in onCreate normally
    view.findViewById(R.id.some_view);

    //Finally, give the custom view to the AlertDialog builder
    builder.setView(view);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM