上篇文章完美解决EditText和ScrollView的滚动冲突(上)中提到咱们自己写了一个判断EditText是否可以在垂直方向上滚动的方法,那么这个方法是如何得来的呢?
其实Android API里是有一个判断控件是否可以在垂直方向上滚动的方法的,方法名字叫做canScrollVertically(int direction),代码如下:

/**
* Check if this view can be scrolled vertically in a certain direction.
*
* @param direction Negative to check scrolling up, positive to check scrolling down.
* @return true if this view can be scrolled in the specified direction, false otherwise.
*/

public boolean canScrollVertically(int direction) {
final int offset = computeVerticalScrollOffset();
final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
if (range == 0) return false;
if (direction < 0) {
return offset > 0;
} else {
return offset < range - 1;
}
}

根据注释不难得知此方法是用来判断当前控件是否可以在垂直的方向上进行滚动的:当参数direction传的是负值的时候,会判断当前控件是否可以向上滚动;否则当参数direction传的是非负值的时候,会判断当前控件是否可以向下滚动。
由此得知我们完全可以利用此方法这样来判断一个控件是否可以在垂直方向上进行滚动:

if(editText.canScrollVertically(-1) || editText.canScrollVertically(0)) {
//垂直方向上可以滚动
}

那么为什么不使用此方法呢?很无奈,因为这个方法是在API 14(也就是Android4.0)才提供的方法,而很多时候我们需要兼容4.0以下的手机,所以并不能直接使用。虽然不能直接使用此方法,不过我们可以看一下它内部是怎么实现的,直接抄过来不就得了!不过还有个悲剧的消息,computeVerticalScrollOffset()、computeVerticalScrollRange()和computeVerticalScrollExtent()这三个方法都是protected方法,所以我们仍然不能使用,没办法,我们只好一块儿将这三个方法内部的实现都看一下。

1.computeVerticalScrollOffset()方法

首先是computeVerticalScrollOffset()方法:`

protected int computeVerticalScrollOffset() {
return mScrollY;
}

此方法定义在View中,并且EditText和TextView都没有重写,所以其返回的必然是mScrollY。那么不适用这个方法我们该如何得到mScrollY呢?稍微猜测一下,既然有mScrollY这么一个变量,那么就应该有其的get方法。查看API,不难发现View中确实有个getScrollY()方法:

public final int getScrollY() {
return mScrollY;
}

2. computeVerticalScrollRange()方法

OK,第一个方法的值我们通过getScrollY()拿到了,接下来咱们来看第二个方法computeVerticalScrollRange():

protected int computeVerticalScrollRange() {
return getHeight();
}

在View中很快找到了此方法,但此方法使我们需要的吗?不要忘了我们使用的是EditText!所以我们需要查看一下在EditText和TextView中是否对此方法进行了重载。不出我们所料,这个方法还真在TextView中进行了重载:

@Override
protected int computeVerticalScrollRange() {
if (mLayout != null)
return mLayout.getHeight();

return super.computeVerticalScrollRange();
}

这个方法返回的是mLayout的高度,那么我们怎么获得mLayout呢?刚刚咱们获得mScrollY时使用了getScrollY()方法,那么是不是会有一个getLayout()方法呢?抱着试试看的态度,忽然间发现在TextView中还真有这么一个方法:

public final Layout getLayout() {
return mLayout;
}

3.computeVerticalScrollExtent()方法

恩,第二个方法的值我们也通过getLayout().getHeight()方法拿到了,现在咱们就来看一下最后一个方法computeVerticalScrollExtent():

protected int computeVerticalScrollExtent() {
return getHeight();
}

在View中我们同样找到了此方法,但根据第二个方法的经验,我们还应该去EditText和TextView中看一下有没有重载。又一次地不出我们所料,这个方法果然在TextView中进行了重载:

@Override
protected int computeVerticalScrollExtent() {
return getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom();
}

然后不难发现,此处使用的三个方法getHeight()、getCompoundPaddingTop()和getCompoundPaddingBottom()都是public方法,我们直接调用即可。
至此,我们已经可以完全对canScrollVertically(int direction)这个方法进行重写了,而重写之后的方法就是咱们上一篇博客中使用的canVerticalScroll(EditText editText)方法。

更多相关文章

  1. android studio 无法调试安装到小米手机解决方法
  2. OpenGL es 2.0使用shadow mapping方法制作阴影时,阴影穿透实体现
  3. Android常用控件学习(1)
  4. Android studio Run 出现 红叉 AndroidRunConfigurationType 问
  5. Android控件组合应用四
  6. Android自定义控件——开源组件SlidingMenu的项目集成
  7. 如何在Log中模拟方法e
  8. fragment 状态保存时怎么执行一些需要在onResume、onPause方法里
  9. 同时兼容高低版本的setBackground跟setTextColor方法

随机推荐

  1. 一些Andriod相关的网站
  2. Android子线程与子线程的通信
  3. Android基本之UI Layout
  4. Android实现电话状态监控
  5. android 播放视频保存的一些网页
  6. android多选联系人实现
  7. 有关Material Design新特性的详解。
  8. Power Profiles for Android
  9. Android(安卓)内存溢出解决方案(OOM) 整理
  10. android sqilte3数据库大小的测试