- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
是否可以在 Fragment 之间切换而无需一直重新创建它们?如果是,怎么办?
In the documentation我找到了一个如何替换 fragment 的例子。
// 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();
但我不想每次需要时都从头开始创建我的 fragment 。
我还找到了this example隐藏/显示 fragment :
// 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 的 fragment 呢?
我认为这可能与 this question 有关, 但没有答案。 :/
非常感谢您,水母
编辑:
我现在就是这样做的:
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();
如果我调出设置,然后调出其他东西,然后返回设置, toast 首先给我“null”,然后是“Settings:SettingsFragment{40ef...”。
但是,如果我将 fragmentTransaction.add(R.id.framelayout_content, set, SET);
替换为 fragmentTransaction.replace(R.id.framelayout_content, set, SET);
我一直收到“null”、“null”、“null”...所以它似乎没有通过标签找到 fragment 。
编辑2:
添加 fragmentTransaction.addToBackStack(null);
成功了。 :)这节省了整个隐藏/内存显示哪个 fragment 的部分,所以我认为这是最优雅的解决方案。
我找到了 this教程对该主题很有帮助。
编辑3:
查看我的代码,我意识到我可以去掉一些部分,所以我将其更改为:
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: Fragment already added
,与here 非常相似.有没有简单的方法来防止这种情况?否则我想我可能会切换回隐藏/显示位。
最佳答案
这可能取决于您试图避免重新创建的内容。
// 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 中点击后退按钮时,将显示前一个 fragment (您将获得一个 onCreateView、onActivityCreated 但没有 onCreate),因此该 fragment 不会被重新创建。至于你的 newFragment,如果你打算再次使用它来更新任何内部状态,你仍然可以保留它,比如 onCreate 或 onActivityCreated。
编辑:
如果您只是有一个菜单列表,每个条目在右 Pane 中调用不同的 fragment ,那么添加到返回堆栈并不是您想要的。为此,您可以预先在每个 fragment 上调用 add(...)
并根据需要简单地隐藏/显示每个 fragment (我没有对此进行测试)。否则我会建议保留对每个 fragment 的引用,在选择不同的菜单项时调用 replace(...)
确保您不会添加到返回堆栈。
关于Android Honeycomb : How to change Fragments in a FrameLayout, 没有重新创建它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6185272/
编辑:为了澄清,我想问的是:在什么情况下您会更喜欢一种语法而不是另一种语法? 有什么区别: .trigger('change') 和 .change() 两者都按预期工作。在任何情况下语法都会有所不同
这个问题在这里已经有了答案: Difference between .on('click') vs .click() (12 个答案) 关闭 6 年前。 有什么区别: $('选择器').change
我用的是Select2-4.0.0 和 $gameSelect.select2().on("change",function(e){....} 工作正常。 但是当我将它链接起来时('change')就
有一天在#haskell 上,有人提到了当字符串改变时字符串的类型应该如何改变的概念。这让我想起了我项目中的一些代码。它一直困扰着我,我说不清为什么。我现在推测,原因是我没有实现这个概念。这是下面的代
我使用了 .on("change") 事件函数,因为我的整个代码中有一部分是动态变化的。 .trigger("change") 在 .change() 中工作正常,但在 .on("change") 中
下面是一个非常简单的表单下拉列表设置。但是,on-change 事件拒绝触发...除非它更改为 ng-change。 这让我卡住了大约一个小时,因为我们在网站的其他地方使用了相同的设置(即模型属性/列
我有两个v-model 案例一: 这很好用 案例二: 即使改变 u1 也会触发 onDateChange(); 最佳答案 :change 绑定(bind)属性,如 v-bind:change=
我找到了 .and方法对于链接许多期望非常有用。 expect { click_button 'Update Boilerplate' @boilerplate_original.reload
出于合规性原因,我需要捕获所有数据库更改。我知道 Change Feed 存储此信息(并且我正在等待完全保真度来捕获删除)。目前,我一直在通过 Function 触发器读取 Change Feed 并
我添加了一个data-ng-change='getSubjectsClasswise(classBean.class_id);'上课标签,但主题未在主题 处加载标签。 一切看起来都很好,没有遇到问题
我有一组复选框,当您单击其中一个时,它们应该全部被选中。 当用户单击一个复选框时,它会检查以该类名称开头的所有其他复选框。我想要的是用户单击一个复选框,并且每次单击仅触发一次 $(".atpSelec
我在 Stack Overflow 上阅读了很多有关此问题的内容,并应用了所有建议的解决方案(getShell pack、布局、getparent 布局等...),但没有一个起作用。 我有一个带有文本
我想更改我的索引。我的数据框如下: partA = pd.DataFrame({'u1': 2, 'u2': 3, 'u3':4, 'u4':29, 'u5':4, 'u6':1, 'u7':323,
我有一个像这样的下拉菜单: Grade Year 旁边还有另一个下拉菜单: 3 4
这个问题已经有人问过,但我只停留在最基本的层面上。除了选择标记和尝试通过 jquery 捕获更改事件外,我没有向我的 html 添加任何内容。这是我的代码: $('#target').bin
我只是 Django 的新手几天。现在,当自定义表单中其他字段的值发生变化时,我需要同时更改一个字段中的值和表示形式。此时更改 MyModel 是受限。 我的应用程序/models.py: class
我正在使用 ListView 控件来显示一些数据行。有一个后台任务接收列表内容的外部更新。新收到的数据可能包含更少、更多或相同数量的项目,而且项目本身可能已更改。 ListView.ItemsSour
我在 android studio 中使用 git 插件。我的问题是当我提交更改列表(公开提交)时,但我在更改列表中的评论是错误的/丢失的,我想更改它。 问题: 有没有办法通过 AndroidStud
MyCustomObject * object=new MyCustomObject(); 假设我的许多类都使用了对象指针,但突然间我想在不更改地址的情况下更改指针的内容。 我认为 object =
我正在使用新的 KeyValue Observing。当变量发生变化时,我接到了我的观察者的电话,但 change struct 附带 newValue和 oldValue都为 nil ,所以它永远不
我是一名优秀的程序员,十分优秀!