在onResume相同的片段重新膨胀不同的布局查看 [英] Re-inflate different layout View in same Fragment in onResume

查看:223
本文介绍了在onResume相同的片段重新膨胀不同的布局查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段 MainFragment ,它可以包含两个,三个或四个嵌套的片段。将要示出的特定片段可通过在设置的用户改变。

有对于片段的每个号码不同的布局。例如, layout_3 当用户选择三个嵌套片段被使用。

我需要做的是动态更新 MainFragment 的布局,以及碎片将被嵌套在该布局中,在 onResume()的一旦用户在设置回来)。大约有10个片段,用户可以从中选择,我需要能够交换他们进出的 MainFragment 动态。

我无法这样做。有一次我从设置恢复到更新的布局/视图的唯一方法就是离开 MainFragment ,然后再回来(它调用 onCreateView())。

下面是我在做一个例子 onCreateView()初始化布局(两个嵌套片段是默认值):

  MVIEW = mInflater.inflate(R.layout.layout_2,mParent,FALSE);
。getChildFragmentManager()调用BeginTransaction()加(R.id.fragmentContainer1,片段1).commit();
。getChildFragmentManager()调用BeginTransaction()加(R.id.fragmentContainer2,fragment2).commit();
返回视图。

假设然后用户前进到的设置,选择有三个嵌套片段。这是我在 onResume已经试过(),没有效果:

  MVIEW = mInflater.inflate(R.layout.layout_3,mParent,FALSE);
。getChildFragmentManager()调用BeginTransaction()加(R.id.fragmentContainer1,片段1))提交()。
。getChildFragmentManager()调用BeginTransaction()加(R.id.fragmentContainer2,fragment2).commit();
。getChildFragmentManager()调用BeginTransaction()加(R.id.fragmentContainer3,fragment3).commit();

我不知道如果我做错了什么。理想情况下,我只想逼 MainFragment 调用 onCreateView()一遍,但没有对这一问题的解决似乎的工作。

任何想法?感谢您的帮助。

编辑:我相信问题是夸大了新的查看,而不是替换片段。

例如,假设默认的屏幕为 layout_4 ,有四个片段的容器。然后,用户进入设置未检查所有四个默认片段,并选择三个新的片段。在 onResume(),我们尽量抬高 layout_3 ,然后添加片段。我觉得 layout_3 从未膨胀,的的,因为我的片段容器具有相同风格的 ID 跨越布局( fragmentContainer1 - fragmentContainer4 ),前三个片段容器更新。第四个依然像现在这样,因为我认为我们在 layout_3 并没有尝试更新它。

此行​​为被证实和结果在碰撞时用户尝试的增加的片段的数目,而不是降低。最重要的,当用户从4个片段切换到三个片段,没有出现崩溃,因为所有的三个片段容器我试图更新 layout_4 存在。但是,如果用户在 layout_2 ,然后转到设置,选择第三个片段,我们会尝试将片段添加到 fragmentContainer3 当我们重新开始。这导致崩溃,因为 layout_3 不膨胀。

了java.lang.RuntimeException:无法恢复活动

java.lang.IllegalArgumentException异常:未找到标识观点0x7f0c005f

任何想法如何解决这一问题?呼叫重新膨胀 MVIEW onResume()似乎没有任何效果。

编辑2:我已经打过电话 mParent.addView(MVIEW)充气后,但仍遇到相同的行为同上,但的大部分。


解决方案

当您从设置恢复, onResume()应该在 MainFragment ,随后即已经装载在 MainFragment 任何嵌套的片段。你可以不包括任何更新逻辑在嵌套的片段 onResume(),而不是只在 onCreateView() <? / p>

否则,您可以创建不同的code路径,并把更新逻辑有:使类公共方法片段1 fragment2 fragment3 ,包括所有的更新逻辑,并呼吁从什么地方这些方法在 MainFragment 。 (你可以创建一个接口,并有嵌套的片段类继承该接口,如果它们是不同的类,你希望有一个更清洁的设计。)

要小心是否将嵌套片段已恢复尚未调用查看方式对象时,嵌套的片段 onResume()还未被称为可能是有问题的。

I have a Fragment, MainFragment, which can contain two, three, or four nested fragments. The specific fragments that will be shown can be changed by the user in the settings.

There is a different layout for each number of fragments. For instance, layout_3 is used when the user chooses three nested fragments.

What I need to do is dynamically update MainFragment's layout, and which fragments will be nested within that layout, in onResume() (i.e. Once the user comes back from the settings). There are about 10 fragments the user can choose from, and I need to be able to swap them in and out of MainFragment dynamically.

I'm having trouble doing this. The only way to update the layout/view once I return from the settings is to leave MainFragment and then come back (which calls onCreateView()).

Here is an example of what I do in onCreateView() to initialize the layouts (two nested fragments is the default):

mView = mInflater.inflate(R.layout.layout_2, mParent, false);
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer1, fragment1).commit();
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer2, fragment2).commit();
return view;

Suppose the user then goes to the settings and chooses to have three nested fragments. This is what I've tried in onResume(), to no effect:

mView = mInflater.inflate(R.layout.layout_3, mParent, false);
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer1, fragment1)).commit();
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer2, fragment2).commit();
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer3, fragment3).commit();

I'm not sure if I'm doing something wrong. Ideally, I would just force MainFragment to call onCreateView() again, but none of the solutions for that problem seem to work.

Any ideas? Thanks for the help.

Edit: I believe the problem is with inflating the new View, rather than replacing the fragments.

For instance, suppose the default screen is layout_4, with four fragment containers. The user then goes to the settings un-checks all four default fragments, and chooses three new fragments. In onResume(), we try to inflate layout_3, and then add the fragments. I think layout_3 never inflates, but because my fragment containers have the same style id across layouts (i.e. fragmentContainer1 - fragmentContainer4), the first three fragment containers are updated. The fourth one remains as it was, since I assumed we were in layout_3 and did not try to update it.

This behavior is confirmed and results in a crash when the user tries to increase the number of fragments, rather than decrease. Above, when the user switched from four fragments to three fragments, there was no crash because all three fragment containers I tried to update exist in layout_4. But if the user is in layout_2 and then goes to the settings to select a third fragment, we'll try to add a fragment to fragmentContainer3 when we resume. This results in a crash because layout_3 fails to inflate.

java.lang.RuntimeException: Unable to resume activity

java.lang.IllegalArgumentException: No view found for id 0x7f0c005f

Any ideas how to fix this? The call to re-inflate mView in onResume() does not seem to have any effect.

Edit 2: I've tried calling mParent.addView(mView) after inflating, but still experience the same behavior as above, for the most part.

解决方案

When you return from the settings, onResume() should be called in the MainFragment and subsequently any nested fragments that were already loaded in the MainFragment. Can you not include any update logic in the nested fragments' onResume() instead of only in onCreateView()?

Otherwise you can create a different code path and put update logic there: make public methods in the classes for fragment1, fragment2, fragment3 that include all your update logic, and call those methods from somewhere in MainFragment. (You could create an interface and have the nested fragment classes inherit that interface, if they are different classes and you want to have a cleaner design.)

Be careful about whether or not the nested fragments have been resumed yet—calling methods on View objects when nested fragments' onResume() hasn't been called yet could be problematic.

这篇关于在onResume相同的片段重新膨胀不同的布局查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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