Android 操作栏.selectTab()和.setSelectedNavigationItem()不工作

Android 操作栏.selectTab()和.setSelectedNavigationItem()不工作,android,tabs,android-actionbar,spinner,android-tabs,Android,Tabs,Android Actionbar,Spinner,Android Tabs,我有一个带有多个选项卡的操作栏,每个选项卡都链接到一个片段。我的问题是,当我使用bar.selectTab(Tab)或bar.setSelectedNavigationItem(int)时,它不起作用。具体来说,当选项卡在ActionBar中缩减为微调器时,就会出现此问题。ActionBar存在一个已知的bug,特别是使用上述方法,特别是当ActionBar的选项卡缩减为微调器时 这是我的解决办法。如果选项卡已缩减为微调器,它将使用反射钻入操作栏。在活动类中,创建如下方法: /** * A d

我有一个带有多个选项卡的操作栏,每个选项卡都链接到一个片段。我的问题是,当我使用
bar.selectTab(Tab)
bar.setSelectedNavigationItem(int)
时,它不起作用。具体来说,当选项卡在ActionBar中缩减为微调器时,就会出现此问题。

ActionBar存在一个已知的bug,特别是使用上述方法,特别是当ActionBar的选项卡缩减为微调器时

这是我的解决办法。如果选项卡已缩减为微调器,它将使用反射钻入操作栏。在活动类中,创建如下方法:

/**
 * A documented and yet to be fixed bug exists in Android whereby
 * if you attempt to set the selected tab of an action bar when the
 * bar's tabs have been collapsed into a Spinner due to screen
 * real-estate, the spinner item representing the tab may not get
 * selected. This bug fix uses reflection to drill into the ActionBar
 * and manually select the correct Spinner item 
 */
private void select_tab(ActionBar b, int pos) {
    try {
        //do the normal tab selection in case all tabs are visible
        b.setSelectedNavigationItem(pos);

        //now use reflection to select the correct Spinner if
        // the bar's tabs have been reduced to a Spinner

        View action_bar_view = findViewById(getResources().getIdentifier("action_bar", "id", "android"));
        Class<?> action_bar_class = action_bar_view.getClass();
        Field tab_scroll_view_prop = action_bar_class.getDeclaredField("mTabScrollView");
        tab_scroll_view_prop.setAccessible(true);
        //get the value of mTabScrollView in our action bar
        Object tab_scroll_view = tab_scroll_view_prop.get(action_bar_view);
        if (tab_scroll_view == null) return;
        Field spinner_prop = tab_scroll_view.getClass().getDeclaredField("mTabSpinner");
        spinner_prop.setAccessible(true);
        //get the value of mTabSpinner in our scroll view
        Object tab_spinner = spinner_prop.get(tab_scroll_view);
        if (tab_spinner == null) return;
        Method set_selection_method = tab_spinner.getClass().getSuperclass().getDeclaredMethod("setSelection", Integer.TYPE, Boolean.TYPE);
        set_selection_method.invoke(tab_spinner, pos, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

ActionBar有一个已知的bug,特别是上面提到的方法,特别是当ActionBar的选项卡被缩减为微调器时

这是我的解决办法。如果选项卡已缩减为微调器,它将使用反射钻入操作栏。在活动类中,创建如下方法:

/**
 * A documented and yet to be fixed bug exists in Android whereby
 * if you attempt to set the selected tab of an action bar when the
 * bar's tabs have been collapsed into a Spinner due to screen
 * real-estate, the spinner item representing the tab may not get
 * selected. This bug fix uses reflection to drill into the ActionBar
 * and manually select the correct Spinner item 
 */
private void select_tab(ActionBar b, int pos) {
    try {
        //do the normal tab selection in case all tabs are visible
        b.setSelectedNavigationItem(pos);

        //now use reflection to select the correct Spinner if
        // the bar's tabs have been reduced to a Spinner

        View action_bar_view = findViewById(getResources().getIdentifier("action_bar", "id", "android"));
        Class<?> action_bar_class = action_bar_view.getClass();
        Field tab_scroll_view_prop = action_bar_class.getDeclaredField("mTabScrollView");
        tab_scroll_view_prop.setAccessible(true);
        //get the value of mTabScrollView in our action bar
        Object tab_scroll_view = tab_scroll_view_prop.get(action_bar_view);
        if (tab_scroll_view == null) return;
        Field spinner_prop = tab_scroll_view.getClass().getDeclaredField("mTabSpinner");
        spinner_prop.setAccessible(true);
        //get the value of mTabSpinner in our scroll view
        Object tab_spinner = spinner_prop.get(tab_scroll_view);
        if (tab_spinner == null) return;
        Method set_selection_method = tab_spinner.getClass().getSuperclass().getDeclaredMethod("setSelection", Integer.TYPE, Boolean.TYPE);
        set_selection_method.invoke(tab_spinner, pos, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

下面是一个可以与Action Bar Sherlock一起使用的版本:。非常感谢您的修复。请提供Bug的链接,我使用了rmirabelle完全共享的功能,但不适用于我。我正在使用Android操作栏,我正在检查Kitkat。在我的情况下,tab\u scroll\u视图变为空。我是否需要更改getDeclaredField(“mTabScrollView”)中声明的字符串?很抱歉,我不确定
mTabScrollView
应该是组件中
ScrollView
的Android内部标识符。这似乎意味着选项卡并没有“缩减”为
微调器
,这里有一个版本可以与动作栏夏洛克一起使用:。非常感谢您的修复。请提供Bug的链接,我使用了rmirabelle完全共享的功能,但不适用于我。我正在使用Android操作栏,我正在检查Kitkat。在我的情况下,tab\u scroll\u视图变为空。我是否需要更改getDeclaredField(“mTabScrollView”)中声明的字符串?很抱歉,我不确定
mTabScrollView
应该是组件中
ScrollView
的Android内部标识符。这似乎意味着选项卡没有“缩减”为
微调器