为了实现左右翻页的效果,使用了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