Skip to content

Instantly share code, notes, and snippets.

@toulouse
Created May 11, 2012 04:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toulouse/2657485 to your computer and use it in GitHub Desktop.
Save toulouse/2657485 to your computer and use it in GitHub Desktop.
Hack around collapsed tab dropdown selection bug in ICS and ActionBarSherlock
@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();
}
}

Boilerplate

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

TL;DR

Assume this code is public domain or something similarly permissive.

@toulouse
Copy link
Author

  • You need to implement OnPageChangeListener
  • And set it in your viewPager: viewPager.setOnPageChangeListener(this); (the fact that it's "this" is arbitrary)
  • Then use the code above (I'd appreciate the credit staying in there if you do use it)

@dcormier
Copy link

This has a bug when the current selected spinner value is smaller than the one selected after setSelectionMethod.invoke(...). The new selected value has the text cut off. Invoking refreshLayout after setSelection solves this problem.

    Method requestLayoutMethod = mTabSpinner.getClass().getSuperclass().getDeclaredMethod("requestLayout");
    requestLayoutMethod.invoke(mTabSpinner);

For the complete modified example, see here: https://gist.github.com/3345940

@toulouse
Copy link
Author

Merged fix, added license.

@MiracleJ
Copy link

Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment