Android ICS +操作栏选项卡方向改变
我设法制作了一个 ActionBar Tab 菜单,调用扩展 Fragments 的不同类。 问题是,当我改变方向时,菜单项之间的切换不起作用。 但我终于弄清楚了问题所在。
这里的主要问题是当方向改变时旧片段不会被删除,因此在用户选择的选项卡上方总是有一个未使用的选项卡的副本
有什么想法吗?我缺少一些基本的东西吗?
谢谢
I managed to make an ActionBar Tab menu, calling different classes who extend Fragments.
The problem is, when I change the orientation, switching between menu items does nothing.
But I finally figured out the problem.
The main issue here is old fragment don't being removed when orientation changes, so there is always a copy of an unused tab just above user's selected tab
Any ideas? I am missing something basic?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于自己找到了解决方案,在我的自定义
ActionBar.TabListener
类中定义的onTabSelected
方法中,我有ft.add
添加了片段以我的观点。当方向更改时,不会调用方法
onTabUnselected
,因此 Fragment 仍保留在那里。将 ft.add 替换为 ft.replace 成功删除了所有旧片段,以便新片段正确显示。
希望这对其他人有帮助
I finally found the solution by myself, in the
onTabSelected
method defined in my customActionBar.TabListener
class I hadft.add
that added the fragment to my View.When the orientation is changed the method
onTabUnselected
was not called, so the Fragment remained there.Replacing
ft.add
toft.replace
managed to erase all old fragments so the new ones where correctly displayed.Hope this helps someone else
我认为最好在活动重新创建时保存 selectedIndex 。
这样你就不会遇到问题,因为不需要选择相同的索引并且取消选择,并且对用户来说也更好。
I think that's better to save selectedIndex on activity recreation.
That way you don't have the problem because the same index is selected and unselected isn't needed and also is nicer for the user.
让选项卡侦听器的构造函数检查旧片段(未调用 onTabUnselected() 留下的片段)并将其分离。这里有一个专门为 ActionBarSherlock 制作的选项卡侦听器: FragmentTabListener.java。 API 演示中的 FragmentTabs.java 中也包含相同的构造函数代码(无需支持库即可使用)。
Have the tab listener's constructor check for the old fragment (left from onTabUnselected() not being called) and detach it. There is a tab listener made specifically for ActionBarSherlock here: FragmentTabListener.java. The same constructor code (for using without the support libraries) is also in FragmentTabs.java from the API demos.