简体   繁体   English

如何通过所需的像素值检测ScrollView滚动

[英]How to detect ScrollView scroll by desired pixel value

I'm extending ScrollView to detect if it was scrolled up or down. 我正在扩展ScrollView以检测它是向上滚动还是向下滚动。 I want to add option to detect scroll only if it was scrolled by let's say 50 pixels. 我想添加仅在滚动了50个像素时才检测滚动的选项。 How to do that? 怎么做? My current code of scrollview overrides onScrollChanged: 我当前的scrollview代码覆盖onScrollChanged:

public interface OnDetectScrollListener {
    void onUpScroll();
    void onDownScroll();
}

public class MyScrollView extends ScrollView {

    private OnDetectScrollListener onDetectScrollListener = null;
    private boolean scrollDown = false;
    private boolean scrollUp = false;

    .... constructors ....

    public void setOnDetectScrollListener(OnDetectScrollListener scrollViewListener) {
        this.onDetectScrollListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (onDetectScrollListener != null) {
            View view = (View) getChildAt(getChildCount() - 1);
            int diff = (view.getBottom() - (getHeight() + getScrollY()));

            //scroll down
            if(t > oldt){
                if(!scrollDown & oldt >= 0 & t >= 0){
                    scrollDown = true;
                    scrollUp = false;
                    onDetectScrollListener.onDownScroll();
                }

            }
            //scroll up
            else if(t < oldt & diff > 0){
                if(!scrollUp){
                    scrollUp = true;
                    scrollDown = false;
                    onDetectScrollListener.onUpScroll();
                }
            }
        }
    }
}

I don't have much experience in ScrollView but you can do this: 我没有ScrollView丰富经验,但是您可以这样做:

if what you want is to start scrolling only after 50 pixels you can follow this logic: 如果您想要的是仅在50像素后才开始滚动,则可以遵循以下逻辑:

bool scrolling = false;
int scrollX = -1;
int scrollY = -1;

protected void scroll(int x, int y)
{
    //View was not scrolling
    if (scrollX == -1)
    {
        //Save starting point
        scrollX = x;
    }
    //View keeps scrolling
    else
    {
        //User is touching 50 pixels left from starting point
        if (x -scrollX > 50)
        {
            scrolling = true;
        } else
        //User is touching 50 pixels right from starting point
        if (scrollX -x > 50)
        {
            scrolling = true;
        }
    }

    if (scrolling)
    {
        /* Your code */
    }
}

I'm not sure if either l or t is x or y on your onScrollView (I've never touched it) but you can implement it your way. 我不确定l或t在您的onScrollView上是x还是y(我从未接触过它),但是您可以按照自己的方式实现它。

Feel free to create separate variables for scrolling left and scrolling right (or up/down). 随意创建用于向左滚动和向右滚动(或向上/向下)的单独变量。

Please avoid using pixels, especially for input. 请避免使用像素,尤其是对于输入。 Prefer using density-independent pixels (dp). 优先使用与密度无关的像素(dp)。 More info here (Supporting Multiple Screens . 此处有更多信息(支持多屏幕)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM