在片段中使用FragmentManager?

时间:2017-01-17 23:32:05

标签: android android-layout android-fragments android-dialogfragment fragmentmanager

我真的遇到了麻烦。我试图从片段中打开一个对话框片段,但我无法让它工作

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class FragmentDiscover extends Fragment
{
    private View rootView;
    private ImageButton folderButton;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.activity_9a_discover , container, false);
        bindActivity();
        return rootView;
    }
    private void bindActivity()
    {
        cardStack = (SwipeDeck) rootView.findViewById(R.id.main_frag_swipe_deck);
        dragCheckbox = (CheckBox) rootView.findViewById(R.id.main_frag_checkbox_drag);
        dragCheckbox.setVisibility(View.GONE);
        //Folder button
        final android.app.FragmentManager fragmentManager = getFragmentManager();

        FragmentManager fragmentManager1 = getFragmentManager();//RED LINE ERROR?!?!?

        final FoldersDialogFragment foldersDialogFragment = new FoldersDialogFragment();
        folderButton = (ImageButton) rootView.findViewById(R.id.main_button_folders);
        folderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                foldersDialogFragment.show(fragmentManager, "folderPop");
            }
        });
        makeSomeSampleCards();
    }

使用FragmentManager fragmentManager1 = getFragmentManager();

foldersDialogFragment.show(fragmentManager, "folderPop");行成为红线错误。

使用FragmentManager fragmentManager1 = getSupportFragmentManager;
使用v4导入,这给我一个红线错误。

我不知道如何解决这个问题......希望你的家伙有智慧!

喝彩!

2 个答案:

答案 0 :(得分:1)

你能展示FoldersDialogFragment的代码吗?

我怀疑当你的片段继承自support.v4.app.Fragment时,你的对话框片段没有。

答案 1 :(得分:1)

当您在Fragment范围内时,使用getChildFragmentManager()检索android.support.v4.app.FragmentManager以添加子片段(其他片段拥有的片段)。

当然,您还应该确保使用android.support.v4.DialogFragment作为FoldersDialogFragment的基类。