问题描述
如果有帮助,我要的是类似的是,在此
If it helps, what I want is similar to what is done in this google tutorial
但是有一个片段到过渡之前创建的。如果我这样做,转变工作正常;但我不能使用这种方法。
But there a fragment is created prior to the transition. If I do that the transition works fine; but I can't use this approach
=====
针对给API 7+我只是想有一个片段,在整个屏幕上可见,用一个按钮(一个拉制按钮,用onTouch事件),那么替代品的第二片段,反之亦然。
Aiming to API 7+ I am just trying to have one Fragment visible in the whole screen and using a button (a drawn button, with an onTouch event) then alternate to a second fragment and viceversa.
不过,我要么得到了一个空白页面,当我更换了与第二第一个片段,或者如果我使用fragmentTransaction.show和fragmentTransaction.hide;我可以切换两次之前,我空白屏幕。我不想对backstack。
But I either get a blank screen when I replace the first fragment with the second, or if I use fragmentTransaction.show and fragmentTransaction.hide; I can switch two times before I get blank screen. I dont want to have on backstack.
我在MainActivity的onCreate创建片段:
I am creating the fragments in MainActivity's 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);
然后在一种方法(从片段调用)我做的开关:
Then on one method (called from the fragments) I do the switch:
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);
}
这是不是我应该怎么做呢?
This is not how I should do this?
替换片段时黑屏
推荐答案
尝试以这种方式来初始化片段:
Try to initialize fragments in that way:
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();
}
然后以这种方式它们之间翻转:
And then flip between them in that way:
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结果片段的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!