為了實現左右翻頁的效果,使用了ViewPager,它會很方便的實現左右滑動后翻頁。
這時需要自己也加上兩個button來實現同樣的操作,如何實現呢?網上一篇blog幫了忙啦。
[【Android】按鈕控制ViewPager的左右翻頁,保留原有的動畫效果](http://blog.sina.com.cn/s/blog_4b20ae2e0101fkpz.html)
ViewPager的一個公共方法arrowScroll,查看代碼我們可以有兩個重要的發現:
~~~
public?boolean?executeKeyEvent(KeyEvent?event)?{??
????boolean?handled?=?false;??
????if?(event.getAction()?==?KeyEvent.ACTION_DOWN)?{??
????????switch?(event.getKeyCode())?{??
????????????case?KeyEvent.KEYCODE_DPAD_LEFT://鍵盤方向鍵左鍵的控制,向左翻頁??
????????????????handled?=?arrowScroll(FOCUS_LEFT);//FOCUS_LEFT:17??
????????????????break;??
????????????case?KeyEvent.KEYCODE_DPAD_RIGHT://右鍵??
????????????????handled?=?arrowScroll(FOCUS_RIGHT);//FOCUS_RIGHT:66??
????????????????break;??
????????????case?KeyEvent.KEYCODE_TAB:??
????????????????if?(Build.VERSION.SDK_INT?>=?11)?{??
????????????????????//?The?focus?finder?had?a?bug?handling?FOCUS_FORWARD?and?FOCUS_BACKWARD??
????????????????????//?before?Android?3.0.?Ignore?the?tab?key?on?those?devices.??
????????????????????if?(KeyEventCompat.hasNoModifiers(event))?{??
????????????????????????handled?=?arrowScroll(FOCUS_FORWARD);//FOCUS_FORWARD:2??
????????????????????}?else?if?(KeyEventCompat.hasModifiers(event,?KeyEvent.META_SHIFT_ON))?{??
????????????????????????handled?=?arrowScroll(FOCUS_BACKWARD);//FOCUS_BACKWARD:1??
????????????????????}??
????????????????}??
????????????????break;??
????????}??
????}??
????return?handled;??
}??
~~~
~~~
public?boolean?arrowScroll(int?direction)?{??
????View?currentFocused?=?findFocus();??
????if?(currentFocused?==?this)?currentFocused?=?null;??
??
????boolean?handled?=?false;??
??
????View?nextFocused?=?FocusFinder.getInstance().findNextFocus(this,?currentFocused,??
????????????direction);??
????if?(nextFocused?!=?null?&&?nextFocused?!=?currentFocused)?{??
????????if?(direction?==?View.FOCUS_LEFT)?{??
????????????//?If?there?is?nothing?to?the?left,?or?this?is?causing?us?to??
????????????//?jump?to?the?right,?then?what?we?really?want?to?do?is?page?left.??
????????????final?int?nextLeft?=?getChildRectInPagerCoordinates(mTempRect,?nextFocused).left;??
????????????final?int?currLeft?=?getChildRectInPagerCoordinates(mTempRect,?currentFocused).left;??
????????????if?(currentFocused?!=?null?&&?nextLeft?>=?currLeft)?{??
????????????????handled?=?pageLeft();??
????????????}?else?{??
????????????????handled?=?nextFocused.requestFocus();??
????????????}??
????????}?else?if?(direction?==?View.FOCUS_RIGHT)?{??
????????????//?If?there?is?nothing?to?the?right,?or?this?is?causing?us?to??
????????????//?jump?to?the?left,?then?what?we?really?want?to?do?is?page?right.??
????????????final?int?nextLeft?=?getChildRectInPagerCoordinates(mTempRect,?nextFocused).left;??
????????????final?int?currLeft?=?getChildRectInPagerCoordinates(mTempRect,?currentFocused).left;??
????????????if?(currentFocused?!=?null?&&?nextLeft?
????????????????handled?=?pageRight();??
????????????}?else?{??
????????????????handled?=?nextFocused.requestFocus();??
????????????}??
????????}??
????}?else?if?(direction?==?FOCUS_LEFT?||?direction?==?FOCUS_BACKWARD)?{//17?or?1??
????????//?Trying?to?move?left?and?nothing?there;?try?to?page.??
????????handled?=?pageLeft();??
????}?else?if?(direction?==?FOCUS_RIGHT?||?direction?==?FOCUS_FORWARD)?{//66?or?2??
????????//?Trying?to?move?right?and?nothing?there;?try?to?page.??
????????handled?=?pageRight();??
????}??
????if?(handled)?{??
????????playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));??
????}??
????return?handled;??
}??
~~~
也就是說,我們調用arrowScroll方法用參數1或者17就可以實現向左翻頁;參數2或66就可以實現向右翻頁。
后記:
當你的UI中有EditText這種獲得focus的widget時,則必須用17和66,否則要報錯。
- 前言
- 一:文本與布局
- 二:組合控件
- 三:性能測試類
- 四:語音識別
- 五:讀取Excel
- 六:PreferenceActivity使用詳解
- 七:按鈕控制ViewPager的左右翻頁
- 八:Ubuntu下切換JDK版本
- 九:最新Android開發環境(Eclipse+ADT+Android 5.0)
- 十:獲得屏幕物理尺寸、密度及分辨率
- 十一:Android Studio和Gradle
- 十二:Android Studio導入第三方類庫、jar包和so庫
- 十三:APK簽名
- 十四:混淆與反編譯
- 十五:多分辨率適配常用目錄
- 十六:getprop與dumpsys命令
- 十七:Linux下的模擬器硬件加速
- 十八:adb取出安裝在手機中的apk
- 十九:android studio導出jar包(Module)并獲得手機信息
- 二十:兩個開源的圖表/報表控件
- 二十一:Android原型設計工具探索
- 二十二:Android 5.1 SDK下載與配置
- 二十三:Android Studio的NDK開發
- 二十四:橫豎屏切換
- 二十五:模擬器如何重啟?試試Genymotion!
- 二十六:persistableMode與Activity的持久化
- 二十七:Maven編譯開源二維碼掃描項目zxing