如何确保始终显示第一个标签?

时间:2012-11-23 02:02:02

标签: android android-fragments android-actionbar

我有:

  1. 仅显示给定网址的WebView的WebViewFragment。
  2. TabbedWebViewHandler,用于处理创建包含WebViewFragments的选项卡并将其添加到操作栏。
  3. 一个HelpActivity,用于创建和显示4个WebViewFragments(分别用于“更多信息”,“条款和条件”,“信用”和“此应用程序的用途”帮助屏幕)。
  4. 所有这一切都正常,除了,当HelpActivity首次启动时,第一个标签始终只是一个空白黑屏:

    blank Android tab

    其他选项卡正常工作,如果选择了另一个选项卡,则第一个选项卡(在本例中为“信息”选项卡)将正确呈现其Web视图,然后重新选择“信息”选项卡。

    我创建它后总是选择第一个标签,并使用actionBar.selectTab(newTab);将其添加到操作栏标签中。我知道执行此操作的代码正在运行,因为日志包含“选择第一个选项卡”。

    我也使用TabbedWebViewHandler进行同样的事情(包括只有一个“tab”的活动,因此不显示标签导航),所以我更喜欢修复TabbedWebViewHandler而不是在HelpActivity中添加一种解决方法。

    我正在使用ActionBarSherlock / Android支持库来提供我的标签功能,如果这是相关的。

    如何确保我的活动的第一个标签始终正确显示?

    WebViewFragment

    public class WebViewFragment extends Fragment {
        private static final String TAG = WebViewFragment.class.getSimpleName();
        private String url;
        @Override
        public View onCreateView(
                LayoutInflater inflater,
                ViewGroup container,
                Bundle savedInstanceState) {
            Log.d(TAG, String.format("onCreateView(): URL is '%s'", url));
            View v = inflater.inflate(R.layout.snippet_webview, container, false);
            WebView wv = (WebView) v.findViewById(R.id.webViewWebView);
            wv.loadUrl(url);
            return v;
        }
        public void setUrl(String url) {
            this.url = url;
        }
    }
    

    TabbedWebViewHandler

    public class TabbedWebViewHandler {
        private static final String TAG = "TabbedWebViewHandler";
        private final ActionBar actionBar;
        private final Context hostContext;
        private final FragmentManager fragmentManager;
        public TabbedWebViewHandler(SherlockFragmentActivity host) {
            this.hostContext = (Context) host;
            this.actionBar = host.getSupportActionBar();
            this.fragmentManager = host.getSupportFragmentManager();
        }
        public void addTab(String title, String renderUrl) {
            Tab newTab = makeTab(title, renderUrl);
            actionBar.addTab(newTab);
            // FIXME: buggy! tabs don't show on first load
            if (actionBar.getTabCount() == 1) {
                /* first tab: select it by default */
                Log.d(TAG, "Selecting first tab");
                actionBar.selectTab(newTab);
            }
            if (actionBar.getTabCount() > 1) { 
                /* more than one tab: enable navigating between tabs */
                actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            }
        }
        private Tab makeTab(String title, String renderUrl) {
            WebViewFragment f = 
                    (WebViewFragment) 
                        SherlockFragment.instantiate(
                            hostContext, 
                            WebViewFragment.class.getName());
            f.setUrl(renderUrl);
            ActionBar.TabListener l = new WebViewFragmentTabListener(f);
            Tab newTab = actionBar.newTab();
            newTab.setText(title);
            newTab.setTabListener(l);
            return newTab;
        }
        private class WebViewFragmentTabListener implements ActionBar.TabListener {
            private final WebViewFragment fragment;
            public WebViewFragmentTabListener(WebViewFragment f) {
                this.fragment = f;
            }
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                // replace current fragment with this fragment
                if (ft == null) { 
                    fragmentManager
                        .beginTransaction()
                        .replace(android.R.id.content, fragment)
                        .commit();
                } else {
                    ft.replace(android.R.id.content, fragment);
                }
            }
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                ft.remove(fragment);
            }
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // do nothing
            }
        }
    }
    

    HelpActivity

    public class HelpActivity extends BaseActivity {
        private static final String TAG = "HelpActivity";
        private static final String FURTHER_URL = 
                "file:///android_asset/further_info.html";
        private static final String DISCLAIMER_URL = 
                "file:///android_asset/terms_and_conditions.html";
        private static final String CREDITS_URL = 
                "file:///android_asset/image_credits.html";
        private static final String PURPOSE_URL = 
                "file:///android_asset/purpose.html";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            Log.i(TAG, "onCreate()");
            super.onCreate(savedInstanceState);
            TabbedWebViewHandler twvh = new TabbedWebViewHandler(this);
            twvh.addTab("Info", FURTHER_URL);
            twvh.addTab("Terms", DISCLAIMER_URL);
            twvh.addTab("Credits", CREDITS_URL);
            twvh.addTab("Purpose", PURPOSE_URL);
        }
    
    }
    

    日志输出

    I/HelpActivity(20038): onCreate()
    D/TabbedWebViewHandler(20038): Selecting first tab
    D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
    I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
    D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
    I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
    D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
    I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
    

1 个答案:

答案 0 :(得分:1)

在开始添加标签之前,将导航模式设置为ActionBar.NAVIGATION_MODE_TABS

编辑: 找到了! action bar's selectTab() method中有一段:

if (getNavigationMode() != NAVIGATION_MODE_TABS) {
    mSavedTabPosition = tab != null ? tab.getPosition() : INVALID_POSITION;
    return;
}

如果条形码不在ActionBar.NAVIGATION_MODE_TABS中,则基本上可以防止标签初始化。