Android ActionBar 作为导航条的一个Bug
来源:互联网 发布:淘宝买小样靠谱点推荐 编辑:程序博客网 时间:2024/04/20 22:35
在Android兼容开发包(Support Package)的示例中,演示了ViewPager和ActionBar导航条一起使用的一个示例,该示例的代码目录位于:
android-sdk-windows\extras\android\support\v13\samples\Support13Demos
目录中的
/Support13Demos/src/com/example/android/supportv13/app/ActionBarTabsPager.java
Java类中。
运行界面如下:
如果修改其中的代码,多添加一个ViewPager和Tab导航,当屏幕横屏的时候则Tab导航会自动变为List导航。如下图:
在这种List导航模式下 有个Bug,就是当左右滑动下面的ViewPager的时候,上面的List当前内容不变化,如下图:
原因是如下的函数在List模型下没有更改里面使用的Spinner的当前列表项:
mActionBar.setSelectedNavigationItem(position);
该Bug的修改方式见如下链接:https://android-review.googlesource.com/#/c/32492/如果您无法修改Android系统的代码,则可以通过如下的方式来解决该Bug:
@Overridepublic void onPageScrollStateChanged(int state) {}@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic 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); } 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(); }}
注意:
- 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)
原文转载自 云在千峰: http://yunfeng.sinaapp.com/?p=414#ixzz1yseBE0al
- Android ActionBar 作为导航条的一个Bug
- Android 导航条效果实现(五) ActionBar+Fragment
- Android导航栏ActionBar的详细分析
- Android导航栏ActionBar的详细分析
- TabLayout的一个bug?左右来回滑动几次导航条崩溃
- 一个导航条的例子
- 一个导航条的进化
- [Android] 使用ActionBar的Up导航的简介及一个疑问
- 【Android】Actionbar下拉导航
- Android导航栏--ActionBar
- [android]ActionBar的搜索条使用
- android导航条的制作
- 【Android】使用Actionbar Tab导航
- Android 原生导航 I-ActionBar
- 【Android】使用Actionbar Tab导航
- 一个不错的javascript导航条
- 一个导航条的效果DIV+CSS
- Android 打造风格统一的导航之actionBar
- Oracle查询数据缓存区命中率的查询语句
- Java Doc 建议不要使用 JPasswordField.getText()
- Linux面试题大全
- 多播,组播
- 设计模式--抽象工厂模式
- Android ActionBar 作为导航条的一个Bug
- oracle唯一索引和唯一约束
- Struts2拦截器Interceptor学习笔记
- 求字符串中出现次数最多且最长的子串、连续出现次数最多的子串
- windows自动关机操作及命令
- linux的内核编译与安装
- 价格奥秘-在超市遇见亚当斯密--第八章 金钱真的是一切动机的源头吗?
- 关于Android应用多语言支持实现
- Oracle的字符替换函数translate用法