Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

android - Get scrolling direction in a scrollview?

This should be pretty straightforward but it seems that I missed something --- how do you figure out the direction (left/right/up/down) of a scrolling event for a ScrollView?

The first thing that comes to mind is to store getScrollX() and getScrollY(), then compare them in the next call to the onTouchListener. Is that the right way of doing things?

I'm implementing a custom HorizontalScrollView and I need to disable scrolling and page swipes in one direction only.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Research is telling me that there is no scroll listener for that case.
You could try your approch.
Another thing I would try is to override onScrollChanged (which provides the scroll position before) and so creating your own ScrollView

Update:
This one workes for me. It scrolls to the right but not to the left.

public class CustomScrollView extends HorizontalScrollView {

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onScrollChanged(int w, int h, int ow, int oh) {
        if (w < ow) {
            scrollTo(ow, h);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...