android:setSelectedNavigationItem()是什么意思

rt

actionbar里面选择索引项目用的。这个是在模式为ActionBar.NAVIGATION_MODE_LIST的情况下用的。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-06-13
actionbar里面选择索引项目用的。这个是在模式为ActionBar.NAVIGATION_MODE_LIST的情况下用的。

俗称:乱码
第2个回答  2015-09-06
I have an ActionBar with multiple tabs, each linked to a fragment. The problem I have is that when I use either bar.selectTab(Tab) or bar.setSelectedNavigationItem(int), it doesn't work. Specifically, this problem occurs when the tabs get reduced down to a Spinner in the 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();
}}
Example usage of this might be:private void delete_fragment_and_tab(String fragment_tag) {

//remove the fragment
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(getFragmentManager().findFragmentByTag(fragment_tag));

//now remove the tab from the ActionBar
//and select the previous tab
ActionBar b = getActionBar();
Tab tab = b.getSelectedTab();
bar.removeTab(tab);
select_tab(bar, bar.getNavigationItemCount() -1); } Here's a version that will work with Action Bar Sherlock: gist.github.com/nolanlawson/5975552 . Thanks a million for this fix. – nlawson Jul 11 '13 at 13:44 1 Please provide link to the bug – Daniel Jul 24 '13 at 22:34 code.google.com/p/android/issues/detail?id=59815 – Oleg Vaskevich Jun 9 '14 at 3:51 I used the function exactly shared by rmirabelle but not working for me. I am using the Android Action bar and I am checking in Kitkat. in my case tab_scroll_view becomes null. Do I need to change the string declared in getDeclaredField("mTabScrollView")? – user448250 Jun 4 at 15:26 I'm sorry, I'm uncertain. mTabScrollView is supposed to be Android's internal identifier for the ScrollView within the component. This would seem to imply that the tabs are not getting 'reduced' to a Spinner – rmirabelle Jun 4 at 15:38 add a comment |
相似回答