Android ICS +操作栏选项卡方向改变

发布于 2025-01-01 02:21:38 字数 193 浏览 4 评论 0原文

我设法制作了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夢归不見 2025-01-08 02:21:38

我终于自己找到了解决方案,在我的自定义 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 custom ActionBar.TabListener class I had ft.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 to ft.replace managed to erase all old fragments so the new ones where correctly displayed.

Hope this helps someone else

夜吻♂芭芘 2025-01-08 02:21:38

我认为最好在活动重新创建时保存 selectedIndex 。
这样你就不会遇到问题,因为不需要选择相同的索引并且取消选择,并且对用户来说也更好。

    protected void onSaveInstanceState(Bundle outState) {   
      super.onSaveInstanceState(outState);
      int i = getActionBar().getSelectedNavigationIndex();
      outState.putInt("selectedTabIndex", i);       
}

    //And then restore
    private void initActionBar(Bundle savedInstanceState) {
        ActionBar ab = getActionBar();
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ab.addTab(...);
            ...

        if(savedInstanceState != null) {
            int index = savedInstanceState.getInt("selectedTabIndex");
            getActionBar().setSelectedNavigationItem(index);
        }   

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.

    protected void onSaveInstanceState(Bundle outState) {   
      super.onSaveInstanceState(outState);
      int i = getActionBar().getSelectedNavigationIndex();
      outState.putInt("selectedTabIndex", i);       
}

    //And then restore
    private void initActionBar(Bundle savedInstanceState) {
        ActionBar ab = getActionBar();
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ab.addTab(...);
            ...

        if(savedInstanceState != null) {
            int index = savedInstanceState.getInt("selectedTabIndex");
            getActionBar().setSelectedNavigationItem(index);
        }   
超可爱的懒熊 2025-01-08 02:21:38

让选项卡侦听器的构造函数检查旧片段(未调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文