ScrollBy()滚动超过最大 [英] ScrollBy() scrolling over the maximum

查看:165
本文介绍了ScrollBy()滚动超过最大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于到Android不会让我就让它滚动处理上一个TextView(链接到这个问题,以尽快我写它补充说)我需要处理它自己。在code我使用非常简单,和它的作品在一定程度上,一个问题,它只是不断的最大后滚动(羯羊即顶部或底部)。我发现使用canScrollVertically(方向)的作品,但限制了我的API拉特14以上,这是我不想做的事。这是我目前的code:

Due to Android not allowing me to just let it handle scrolling on a TextView (link to that question to be added as soon as I write it) I need to handle it myself. The code i am using is quite simple, and it works to an extent, with a problem, it just keeps scrolling after the maximum (wether that is the top or the bottom). I found using canScrollVertically(direction) works, but that limits me to API lvl 14 and above, which is something i dont want to do. Here is my current code:

float prevY;

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        int dy = (int) (prevY - event.getY());
        if (myView.canScrollVertically(dy)) { //This is the line i want to get rid of
            myView.scrollBy(0, dy);
        }
    }
    prevY = event.getY();

    return true;
}

此外,有没有使其行为completelly像任何滚动视图的方法吗?我的意思是,当你快速滑动并放开,它doesent STO inmediatly,它使速度一段时间,所以滚动长距离doesent采取青睐。

Also, is there any way of making it behave completelly like any Scrolling view? I mean when you swipe quickly and let go, it doesent sto inmediatly, it keeps the speed for a while, so scrolling long distances doesent take ages.

推荐答案

从大卫的回答,以及大量的试验和错误的开始,这里就是doesent允许滚动过去的顶部和底部的一个版本,并从onTouch作品( )我alredy有。

Starting from David's answer, and a lot of trial and error, here is a version that doesent allow scrolling past the top and bottom, and works from the onTouch() I alredy have.

float prevY;

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        int dy = (int) (prevY - event.getY());
        if (dy < 0) {
            int distanceToTop = myView.getScrollY();
            if (-distanceToTop < dy) {
                myView.scrollBy(0, dy);
            } else {
                myView.scrollTo(0, 0);
            }
        } else {
            int realHeight = (myView.getLineCount() + 1)
                    * myView.getLineHeight();
            int distanceToBottom = myView.getScrollY() - realHeight
                    + myView.getHeight();
            if (-distanceToBottom > dy) {
                myView.scrollBy(0, dy);
            } else {
                myView.scrollTo(0, realHeight - myView.getHeight());
            }
        }
    }
    prevY = event.getY();
    return true;
}

请注意,我允许滚动一行比最大进一步发展,从视图底线的底部一定的空间。

Note that i allow scrolling to one line further than the maximum to let some space from the bottom of the view to the bottom line.

但是,它不是一个正常的滚动视图呢,因为它doesent时进行刷卡不断滚动,但它是配不上我,如果没有对方的回答让我这样做。

It is however not what a normal scrolling view does, as it doesent keep scrolling when a swipe is performed, but it is good enough for me if no other answer allows me to do that.

这篇关于ScrollBy()滚动超过最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆