如果有帮助,我想要的类似于此google tutorial 中的操作
但是在过渡之前会创建一个 fragment 。如果我这样做,过渡效果很好;但我不能使用这种方法
=====
针对API 7+,我只是想在整个屏幕上看到一个 fragment ,并使用一个按钮(一个绘制的按钮,带有一个onTouch事件),然后替换为第二个 fragment ,反之亦然。
但是,当我用第二个 fragment 替换第一个 fragment 时,或者如果我使用fragmentTransaction.show和fragmentTransaction.hide,我将得到一个空白屏幕。在出现黑屏之前,我可以切换两次。我不想在后场。
我在MainActivity的onCreate中创建 fragment :
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);
然后使用一种方法(从 fragment 中调用)进行切换:
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);
}
这不是我应该怎么做吗?
更换 fragment 时黑屏
最佳答案
尝试以这种方式初始化 fragment :
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();
}
然后以这种方式在它们之间切换:
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;
}
关于android - android中的 fragment 事务导致黑屏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16546098/