Android强制Fragment重建View
我有一个简单的应用程序,有两个片段,在横向模式下,两个片段并排显示,在纵向模式下,我显示片段 A,然后如果他们选择一个选项,则启动一个显示片段 B 的活动。我的问题是当我我处于纵向模式并显示片段 B,如果用户选择菜单选项,我想刷新或重绘与片段 B 关联的视图,并且无法理解如何进行此操作。我尝试了 getView 方法和 getLayoutInflater 方法,但屏幕没有改变,因为我认为我正在创建一个新视图。我还尝试获取对片段 A 的引用,认为我可以调用其例程来构建新片段并替换片段 B,但无法获取对它的引用,因为它未显示。我真正需要做的只是强制再次调用 onCreateView 方法来膨胀新视图,但我尝试了多种方法来尝试执行此操作,但无法再次调用 onCreateView 。关于如何做到这一点有什么想法吗?
I have a simple app that has two fragments and when in landscape mode, both fragments are shown side by side and in portrait I show Fragment A and then if they select an option, start an Activity that shows Fragment B. My problem is when I am in Portrait mode and showing Fragment B, if the user selects a menu option I want to refresh or redraw the View that is associated with Fragment B and can’t understand how to make this work. I tried the getView method and getLayoutInflater method but the screen doesn’t change because I think I am creating a new view. I also tried to get a reference to Fragment A thinking I could call its routine to build a new fragment and replace Fragment B but can’t get a reference to it because it is not being displayed. What I really need to do is just force the onCreateView method to be called again to inflate the new view but I have tried several methods to try to do this but can’t get the onCreateView to be called again. Any thoughts on how to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要执行
FragmentTransaction
并使用replace()
方法这应该不难做到,但答案将取决于您的菜单所在的位置(即您的
onOptionsItemSelected()
回调是在您的父 Activity 中回调还是在片段 A/B 中?)。为简单起见,假设您的菜单实现和
onOptionsItemSelected()
位于父 Activity 中,并且您希望在选择 menu_option1 时重塑片段的形状。它看起来像这样:或者,如果您的菜单是其中一个片段的子级(这应该是为了更多可重用代码),那么一种方法是要求主机 Activity 实现由片段,可用作回调。
在片段类内的
onOptionsItemSelected()
回调中,您只需调用此回调方法即可。虽然听起来很拗口,但您实际上只需要做几件事。假设这是您的 Fragment 类
然后在 Activity 中,您会看到类似以下内容的内容:
You will want to perform a
FragmentTransaction
and use thereplace()
methodThis shouldn't be too hard to do, but the answer will depend on where your Menu is located (i.e. is your
onOptionsItemSelected()
call back inside your parent Activity or is it in either Fragment A/B?).Suppose for simplicity's sake, your menu implementation and
onOptionsItemSelected()
is in the parent activity, and you want to reshape the fragments in the event that menu_option1 is chosen. It would look something like this:Alternatively, if your menu is a child of one of your Fragments (which it should be for the sake of more reusable code), then one method is to require that host Activity to implement an interface defined by the Fragment, that can be used as a call back.
And in the
onOptionsItemSelected()
callback inside your fragment class, you simply make a call to this callback method.Although it sounds like a mouthful, you only really need to do a couple of things. Pretend that this is your Fragment class
Then in the Activity, you would see something like:
在
onOptionsItemSelected()
中,让 Activity 调用以下方法导致它用新内容更新其小部件的片段。或者,让 Activity 执行FragmentTransaction
来替换片段(如果片段最初是通过FragmentTransaction
设置的)。In
onOptionsItemSelected()
, have the activity call a method on the fragment that causes it to update its widgets with the new content. Or, have the activity execute aFragmentTransaction
to replace the fragment (if the fragment was originally set up via aFragmentTransaction
).