如何在“操作栏”下拉导航中设置活动项目?

我正在尝试解决方向更改重新启动活动的问题。

我有一个带有下拉列表导航的ActionBar ,在每次旋转之后,这个列表的第一个元素被激活。 保持fragment内容并不困难,但我不知道如何设置活动列表项目。

这里是ActionBar的定义:

 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); ArrayAdapter<CharSequence> list = ArrayAdapter .createFromResource(this, R.array.action_list, android.R.layout.simple_dropdown_item_1line); list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); getActionBar().setListNavigationCallbacks(list, this); 

这是我的解决方法:

 @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { if (!application.isRotated) { application.activePosition = itemPosition; application.activeId = itemId; getFragmentManager().beginTransaction() .replace(android.R.id.content, MyFragment.newInstance(itemPosition)) .commit(); } else { application.isRotated = false; this.onNavigationItemSelected(application.activePosition, application.activeId); } return true; } @Override protected void onStop() { super.onStop(); application.isRotated = true; } 

我不确定这是最好的解决scheme。

我刚刚find了这个function。 它是setSelectedNavigationItem(int position)

在列表或标签导航模式下设置选定的导航项目。

例:

 actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); actionBar.setListNavigationCallbacks(adapter, this); actionBar.setSelectedNavigationItem(position); 

从支持库v7开始,您只需要保存/恢复ActionBar的状态:

 private static final String STATE_SELECTED_NAVIGATION_ITEM = "selectedNavItem"; @Override public void onRestoreInstanceState(Bundle savedInstanceState) { // Restore the previously serialized current dropdown position. if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { getSupportActionBar().setSelectedNavigationItem( savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); } } @Override public void onSaveInstanceState(Bundle outState) { // Serialize the current dropdown position. outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getSupportActionBar() .getSelectedNavigationIndex()); } 

我也遇到了同样的问题。 做了大量的研究,我在这里find了解决scheme:

在提示5下http://mohitum.wordpress.com/tutorials/android/ – >。

实现OnPageChangeListener并在onPageSelected(int position)中调用此方法,如下所示:

 @Override public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); selectInSpinnerIfPresent(position, true); } private void selectInSpinnerIfPresent(int position, boolean animate) { try { ActionBar actionBarView = mActionBar; Class<?> actionBarViewClass = actionBarView.getClass(); Field mTabScrollViewField = actionBarViewClass.getDeclaredField(“mTabScrollView”); mTabScrollViewField.setAccessible(true); Object mTabScrollView = mTabScrollViewField.get(actionBarView); if (mTabScrollView == null) { return; } Field mTabSpinnerField = mTabScrollView.getClass().getDeclaredField(“mTabSpinner”); mTabSpinnerField.setAccessible(true); Object mTabSpinner = mTabSpinnerField.get(mTabScrollView); if (mTabSpinner == null) { return; } Method setSelectionMethod = mTabSpinner.getClass().getSuperclass().getDeclaredMethod(“setSelection”, Integer.TYPE, Boolean.TYPE); setSelectionMethod.invoke(mTabSpinner, position, animate); Method requestLayoutMethod = mTabSpinner.getClass().getSuperclass().getDeclaredMethod(“requestLayout”); requestLayoutMethod.invoke(mTabSpinner); } catch (Exception e) { e.printStackTrace(); } } 

我希望这可以帮助别人。

Codesnippet在Gist上


 @Override public void onPageScrollStateChanged(int state) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); selectInSpinnerIfPresent(position, true); } /** * Hack that takes advantage of interface parity between ActionBarSherlock and the native interface to reach inside * the classes to manually select the appropriate tab spinner position if the overflow tab spinner is showing. * * Related issues: https://github.com/JakeWharton/ActionBarSherlock/issues/240 and * https://android-review.googlesource.com/#/c/32492/ * * @author toulouse@crunchyroll.com */ private void selectInSpinnerIfPresent(int position, boolean animate) { try { View actionBarView = findViewById(R.id.abs__action_bar); if (actionBarView == null) { int id = getResources().getIdentifier("action_bar", "id", "android"); actionBarView = findViewById(id); } Class<?> actionBarViewClass = actionBarView.getClass(); Field mTabScrollViewField = actionBarViewClass.getDeclaredField("mTabScrollView"); mTabScrollViewField.setAccessible(true); Object mTabScrollView = mTabScrollViewField.get(actionBarView); if (mTabScrollView == null) { return; } Field mTabSpinnerField = mTabScrollView.getClass().getDeclaredField("mTabSpinner"); mTabSpinnerField.setAccessible(true); Object mTabSpinner = mTabSpinnerField.get(mTabScrollView); if (mTabSpinner == null) { return; } Method setSelectionMethod = mTabSpinner.getClass().getSuperclass().getDeclaredMethod("setSelection", Integer.TYPE, Boolean.TYPE); setSelectionMethod.invoke(mTabSpinner, position, animate); Method requestLayoutMethod = mTabSpinner.getClass().getSuperclass().getDeclaredMethod("requestLayout"); requestLayoutMethod.invoke(mTabSpinner); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } 

这个官方Android Bug的破解对我来说,上面的codesnippet没有:/

这个实现适用于我(@ mohitum007的响应的修改版本):

 public static void selectInSpinnerIfPresent(Object inActionBar, int position, boolean animate) { // get the ActionBar class Class<?> actionBarClass = inActionBar.getClass(); // if it is a Jelly Bean implementation (ActionBarImplJB), get the super // class (ActionBarImplICS) if ("android.support.v7.app.ActionBarImplJB".equals(actionBarClass .getName())) { actionBarClass = actionBarClass.getSuperclass(); } try { // try to get the mActionBar field, because the current ActionBar is // probably just a wrapper Class // if this fails, no worries, this will be an instance of the native // ActionBar class or from the ActionBarImplBase class final Field actionBarField = actionBarClass .getDeclaredField("mActionBar"); actionBarField.setAccessible(true); inActionBar = actionBarField.get(inActionBar); actionBarClass = inActionBar.getClass(); } catch (IllegalAccessException e) { } catch (IllegalArgumentException e) { } catch (NoSuchFieldException e) { } try { Field mTabScrollViewField = actionBarClass .getDeclaredField("mTabScrollView"); mTabScrollViewField.setAccessible(true); Object mTabScrollView = mTabScrollViewField.get(inActionBar); if (mTabScrollView == null) { return; } Field mTabSpinnerField = mTabScrollView.getClass() .getDeclaredField("mTabSpinner"); mTabSpinnerField.setAccessible(true); Object mTabSpinner = mTabSpinnerField.get(mTabScrollView); if (mTabSpinner == null) { return; } Method setSelectionMethod = mTabSpinner .getClass() .getSuperclass() .getDeclaredMethod("setSelection", Integer.TYPE, Boolean.TYPE); setSelectionMethod.invoke(mTabSpinner, position, animate); Method requestLayoutMethod = mTabSpinner.getClass().getSuperclass() .getDeclaredMethod("requestLayout"); requestLayoutMethod.invoke(mTabSpinner); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | IllegalArgumentException | NoSuchFieldException e) { } }