Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 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 Answer

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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...