2012-01-27 77 views
41

我正試圖解決與方向更改重新啓動活動的問題。如何在操作欄下拉導航中設置活動項目?

我有一個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; 
} 

我不知道它是雖然最佳的解決方案。

回答

83

我剛剛發現該功能。它是setSelectedNavigationItem(int position)

將所選導航項目設置爲列表或選項卡式導航模式。

例子:

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

你介意張貼的你是如何得到這個工作的例子嗎?我一直在嘗試像getActionBar()。setSelectedNavigationItem(1)但它沒有任何效果..(其他人在這裏有相同的問題:http://stackoverflow.com/questions/8487425/actionbar-dropdown-spinner-item -defaults-to-first-item) – brk3 2012-02-11 12:54:42

+1

好的,更新了我的答案。此外,你可以看看我的應用程序的源代碼,看看我如何使用它。 https://github.com/Tsukanov/Simple-Counter/blob/master/src/me/tsukanov/counter/CounterActivity.java#L90 – Roman 2012-02-11 16:03:37

+0

謝謝,它的作品!我想我一直在做錯的是在添加適配器之前調用setSelectedNavigationItem。以前肯定我是這麼試過的,但是哦,現在正在工作:) – brk3 2012-02-11 17:44:28

0

我也遇到同樣的問題。製作了大量的研究,我發現這裏的解決方案:

http://mohitum.wordpress.com/tutorials/android/ - >下的提示5.

實施OnPageChangeListener並在使用onPageSelected(INT位置)調用此方法是這樣的:

@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(); 
    } 
    } 

我希望這可以幫助別人。

0

Codesnippet on 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 [email protected] 
*/ 
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臭蟲爲我做的codesnippet上面沒有:/

1

由於支持庫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()); 
} 
0

此實現對我的作品(@ 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) { 
    } 
} 
相關問題