android中片段的事务导致空白屏幕

qui*_*tor 7 android android-layout android-fragments

如果它有帮助,我想要的东西与谷歌教程中的内容类似

但是在转换之前会创建一个片段.如果我这样做,过渡工作正常; 但我不能用这种方法

=====

针对API 7+,我只想在整个屏幕中看到一个片段,并使用一个按钮(一个带有onTouch事件的绘制按钮),然后交替使用第二个片段,反之亦然.

但是当我用第二个片段替换第一个片段时,或者如果我使用fragmentTransaction.show和fragmentTransaction.hide,我得到一个空白屏幕; 在我得到空白屏幕之前,我可以切换两次.我不想在后台上.

我在MainActivity的onCreate中创建片段:

DiceTable diceTable = new DiceTable();
Logger logger = new Logger();
fragmentTransaction.add(diceTable, DICETABLE_TAG);
fragmentTransaction.add(logger, LOGGER_TAG);
fragmentTransaction.add(R.id.fragment_container, logger);
fragmentTransaction.add(R.id.fragment_container, diceTable);
Run Code Online (Sandbox Code Playgroud)

然后在一个方法(从片段调用)我做切换:

    Logger logger = (Logger)fragmentManager.findFragmentByTag(LOGGER_TAG);
    DiceTable diceTable = (DiceTable)fragmentManager.findFragmentByTag(DICETABLE_TAG);

    if (diceTable.isVisible()) {
        fragmentTransaction.replace(R.id.fragment_container, logger);

        fragmentTransaction.commit();
        fragmentTransaction.hide(diceTable);
        fragmentTransaction.show(logger);
    }
    else if (logger.isVisible()) {
        fragmentTransaction.replace(R.id.fragment_container, diceTable);

        fragmentTransaction.commit();
        fragmentTransaction.hide(logger);
        fragmentTransaction.show(diceTable);
    }
Run Code Online (Sandbox Code Playgroud)

这不是我应该怎么做的?

更换碎片时出现空白屏幕

mak*_*tar 6

尝试以这种方式初始化片段:

private void initFragments() {
    mDiceTable = new DiceTable();
    mLogger = new Logger();
    isDiceTableVisible = true;

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragment_container, mDiceTable);
    ft.add(R.id.fragment_container, mLogger);
    ft.hide(mLogger);
    ft.commit();
}
Run Code Online (Sandbox Code Playgroud)

然后以这种方式在它们之间翻转:

 private void flipFragments() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (isDiceTableVisible) {
            ft.hide(mDiceTable);
            ft.show(mLogger);
        } else {
            ft.hide(mLogger);
            ft.show(mDiceTable);
        }
        ft.commit();
        isDiceTableVisible = !isDiceTableVisible;
    }
Run Code Online (Sandbox Code Playgroud)