当前位置: 移动技术网 > 移动技术>移动开发>Android > 完美解决EditText和ScrollView的滚动冲突(下)

完美解决EditText和ScrollView的滚动冲突(下)

2019年07月24日  | 移动技术网移动技术  | 我要评论

上篇文章完美解决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)方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网