Created
June 14, 2013 14:34
-
-
Save lecho/5782313 to your computer and use it in GitHub Desktop.
Hack around collapsed tab dropdown selection bug in ICS and ActionBarSherlock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment