Android的蜂窝:如何更改一个的FrameLayout片段,而无需重新创建它们? [英] Android Honeycomb: How to change Fragments in a FrameLayout, without re-creating them?

查看:152
本文介绍了Android的蜂窝:如何更改一个的FrameLayout片段,而无需重新创建它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能无需重新创建他们所有的时间片段之间进行切换?如果是这样,怎么样?

is it possible to switch between Fragments without re-creating them all the time? If so, how?

在文档我找到了如何更换片段的例子。

In the documentation I found an example of how to replace Fragments.

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

但我不想从从头开始创建我的每一个片段,我需要他们的时间。

But I don't want to create my Fragments from the scratch every time I need them.

我还发现<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentHideShow.html">this隐藏的例子 /显示片段:

I also found this example of hiding/showing Fragments:

// The content view embeds two fragments; now retrieve them and attach
// their "hide" button.
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));

不过,我将如何创建一个XML文件以外的ID的片段?

But how would I create a fragment with an ID outside an XML file?

我想这可能与这个问题,但有ISN T答案。 :/

I think this might be related to this question, but there isn't an answer. :/

非常感谢你在前进, 水母

Thank you very much in advance, jellyfish

编辑:

这就是我现在怎么做:

Fragment shown = fragmentManager.findFragmentByTag(shownFragment);

//...

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);

//switch statetement for menu selection, just one example:

SettingsFragment set = (SettingsFragment) fragmentManager.findFragmentByTag(SET);
Toast.makeText(this, "Settings:" + set, Toast.LENGTH_LONG).show();
if (set == null)
{
        set = new SettingsFragment();
        fragmentTransaction.add(R.id.framelayout_content, set, SET);
}
else fragmentTransaction.show(set);
shownFragment = SET;
fragmentTransaction.commit();

如果我调出设置,然后别的东西,然后返回到设置,敬酒给我空先。设定:SettingsFragment {40ef ......第二个

If I call up the settings, then something else, and then go back to settings, the toast gives me "null" first and "Settings:SettingsFragment{40ef..." second.

不过,如果我替换 fragmentTransaction.add(R.id.framelayout_content,设置,SET); fragmentTransaction.replace(R.id .framelayout_content,设置,SET); 我不断收到空,空,空......所以它似乎并没有找到片段按标签

However, if I replace fragmentTransaction.add(R.id.framelayout_content, set, SET); with fragmentTransaction.replace(R.id.framelayout_content, set, SET); I keep getting "null", "null", "null"... so it doesn't seem to find the Fragment by tag.

EDIT2:

添加 fragmentTransaction.addToBackStack(空); 的伎俩。 :) 这样可以节省整个隐藏/识记该片段显示部分,所以我想这是这个最完美的解决方案。

Adding fragmentTransaction.addToBackStack(null); did the trick. :) This saves the whole hiding/memorizing which fragment is shown part so I suppose it's the most elegant solution for this.

我发现教程的话题非常有帮助。

I found this tutorial quite helpful on the topic.

EDIT3:

看着我的code我意识到我可以摆脱一些地方,所以我改成了:

Looking at my code I realized I could get rid of some parts, so I changed it to:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);
Settings set = (Settings) fragmentManager.findFragmentByTag(SET);
if (set == null) set = new Settings();

fragmentTransaction.replace(R.id.framelayout_content, set, SET);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

不过,这种调用的 IllegalStateException异常:片段已经添加,差不多像<一个href="http://stackoverflow.com/questions/6250580/fragment-already-added-illegalstateexception">here.有没有一种简单的方法,以prevent呢?否则,我想我可能会切换回隐藏/显示位。

However, this invoked an IllegalStateException: Fragment already added, much the same like here. Is there an easy way to prevent this? Otherwise I think I might switch back to the hide/show bit.

推荐答案

这可能取决于你正在努力一下,以免被重新创建。

It could depend on what you are trying to avoid being re-created.

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

在你的榜样,例如,当你打从newFragment后退按钮的previous片段将显示(你会得到一个onCreateView,onActivityCreated但没有的onCreate),这样的片段不被重新创建为这样的。至于你newFragment你仍然可以保持它的周围,如果你打算使用按要求再说一遍更新任何内部状态的onCreate或onActivityCreated。

In your example example when you hit the back button from your newFragment the previous fragment will be shown (you'll get an onCreateView, onActivityCreated but no onCreate) so that fragment isn't being re-created as such. As for you newFragment you can still keep it around if you plan to use it again updating any internal state as required in say onCreate or onActivityCreated.

编辑:

你想要的东西。如果你只是有一个菜单列表,每个条目调用不同的片段,在右窗格中,然后加入到后面堆栈。对于这一点,你可能会逃脱调用添加(...)每个片段的前期,只是隐藏/显示每个片段有规定的(我没有测试过这一点) 。否则,我会建议保持参照每个片段,叫更换(...)上选择不同的菜单项,以确保您没有添加到回栈。

If you simply have a menu list with each entry invoking a different fragment in a right pane then adding to the back stack is not what you want. For this you might get away with calling add(...) on each fragment up-front and simply hide/show each fragment as required (I've not tested this). Otherwise I would suggest holding a reference to each fragment, call replace(...) on selecting a different menu item ensuring that you don't add to the back stack.

这篇关于Android的蜂窝:如何更改一个的FrameLayout片段,而无需重新创建它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆