在DialogFragment中显示Fragment类

时间:2015-03-23 20:43:22

标签: java android android-fragments

Hello stackoverflowwers !!

我想在我的DialogFragment中添加一个Fragment。这是我目前的代码:

CustomDialogFragment.java:

public static CustomDialogFragment newInstance(String searchType){
        CustomDialogFragment frag = new CustomDialogFragment();
        Bundle args = new Bundle();
        args.putString("searchType", searchType);
        frag.setArguments(args);

        Log.d(TAG, "newInstance");

        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        fm = getFragmentManager();

        final View layout = inflater.inflate(R.layout.dummy_fragment, container);

        layout.findViewById(R.id.dummy_fl);

        final Fragment search = new Search(new Searcher(null, getArguments().getString("searchType")), null, null);
        getDialog().setTitle(getResources().getString(R.string.Search));

        onFocusChangeListener = new OnFocusChangeListener(){

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.d(TAG, "Focus changed");
                fm.beginTransaction().replace(layout.findViewById(R.id.dummy_fl).getId(), search, search.getTag()).commit();

            }
        };

        onLayoutChangeListener = new OnLayoutChangeListener(){

            @Override
            public void onLayoutChange(View v, int left, int top, int right,
                    int bottom, int oldLeft, int oldTop, int oldRight,
                    int oldBottom) {
                Log.d(TAG, "Layout changed");
**fm.beginTransaction().add(search, search.getTag()).commit();**

            }


        };

        layout.setOnFocusChangeListener(onFocusChangeListener);

//      for(int i = 0; i < fm.getFragments().size(); i++){
//              Log.d(TAG, String.format("fm.getFragments().get(%d).getView().getRootView().getId())",i) + String.valueOf(fm.getFragments().get(i).getView()));
//              View view = fm.getFragments().get(i).getView(); 
//                          
//              if(view != null){
//                  return view;    
//              }
//          }           
        layout.addOnLayoutChangeListener(onLayoutChangeListener);
        layout.requestFocus();
        return layout;
    }

当我运行应用程序时,它会在标有** **的行上崩溃。

它引发的错误是片段已被添加,我该如何显示该片段?

我尝试了多个“解决方案”,以便在我的DialogFragment中显示片段。上面的“解决方案”是其中之一。 这是另一个:

CustomDialogFragment.java:

public static CustomDialogFragment newInstance(String searchType){
        CustomDialogFragment frag = new CustomDialogFragment();
        Bundle args = new Bundle();
        args.putString("searchType", searchType);
        frag.setArguments(args);

        Log.d(TAG, "newInstance");

        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        fm = getFragmentManager();

        final View layout = inflater.inflate(R.layout.dummy_fragment, container);

        layout.findViewById(R.id.dummy_fl);

        final Fragment search = new Search(new Searcher(null, getArguments().getString("searchType")), null, null);
        getDialog().setTitle(getResources().getString(R.string.Search));





I'm only using one of these codes to add or replace the fragments.

fm.beginTransaction().replace(layout.findViewById(R.id.dummy_fl).getId(), search, search.getTag()).commit(); --> This will return an error because the id R.id.dummy_fl isn't found in the fragment Search. Duhhh, it isn't in the Search fragment.   

fm.beginTransaction().add(search, search.getTag()).commit(); --> This gives the error described above





        for(int i = 0; i < fm.getFragments().size(); i++){
                Log.d(TAG, String.format("fm.getFragments().get(%d).getView().getRootView().getId())",i) + String.valueOf(fm.getFragments().get(i).getView()));
                View view = fm.getFragments().get(i).getView(); 

                if(view != null){
                    return view;    
                }
            }           

        layout.requestFocus();
        return null;
    }

在Search.java片段中,我在onCreateView()视图中添加了一行,在调用onCreateView()视图时输出一个Log。只有上面的'解决方案'显示了日志条目(这是有道理的,因为第一个'解决方案'没有“加载”片段)

但上面代码的结果是视图为null,而CustomDialogFragment中的onCreateView()返回null,导致片段无法显示。

使用片段的主要原因是我在应用程序的其他地方使用此片段,并且需要将这些更改“推送”到两个片段用法。 Sooooo,我不想在每次需要编辑时编辑两个搜索片段。

我该如何解决这个问题?

顺便说一句:当我删除CustomDialogFragment的Fragment添加时,它将完美地显示dummy_layout,其中包含dummy_textView。所以CustomDialogFragment可以显示东西:P

如果您有任何疑问,请与我们联系!

提前致谢,

1 个答案:

答案 0 :(得分:1)

如果要动态添加片段作为子片段,则需要使用ChildFragmentManager

      getChildFragmentManager()
               .beginTransaction()
               .add(R.id.fragment_child, mChildFragment, "tag")
               .commit();