Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Scroller simple example

Can anyone gives me simple example about Scroller class? As I understand, it encapsulates scrolling, so I need start calculating and then manually update must ScrollView to new positions. So I just try

 Scroller scroller = new Scroller(getApplicationContext());
    scroller.startScroll(0, 0, 10, 10, 500);
    for (int i = 0; i < 100; i++) {
        Log.d("scroller", scroller.getCurrX()+" "+ scroller.getCurrY());
    }

All I have in output is just zeros. Where is my mistake?

like image 698
Artem Caritas Avatar asked Jun 14 '12 09:06

Artem Caritas


People also ask

What is scroll view explain with example?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.

Can scroll vertically Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In this above code, we have declare Linear layout as parent and added Vertical Scroll view.

How do you scroll on an emulator?

Left-click and hold, and drag the screen to scroll.


2 Answers

private class Flinger implements Runnable {
    private final Scroller scroller;

    private int lastX = 0;

    Flinger() {
        scroller = new Scroller(getActivity());
    }

    void start(int initialVelocity) {
        int initialX = scrollingView.getScrollX();
        int maxX = Integer.MAX_VALUE; // or some appropriate max value in your code
        scroller.fling(initialX, 0, initialVelocity, 0, 0, maxX, 0, 10);
        Log.i(TAG, "starting fling at " + initialX + ", velocity is " + initialVelocity + "");

        lastX = initialX;
        getView().post(this);
    }

    public void run() {
        if (scroller.isFinished()) {
            Log.i(TAG, "scroller is finished, done with fling");
            return;
        }

        boolean more = scroller.computeScrollOffset();
        int x = scroller.getCurrX();
        int diff = lastX - x;
        if (diff != 0) {
            scrollingView.scrollBy(diff, 0);
            lastX = x;
        }

        if (more) {
            getView().post(this);
        }
    }

    boolean isFlinging() {
        return !scroller.isFinished();
    }

    void forceFinished() {
        if (!scroller.isFinished()) {
            scroller.forceFinished(true);
        }
    }
}

Taken from https://stackoverflow.com/a/6219382/1351347

like image 158
Thkru Avatar answered Sep 30 '22 14:09

Thkru


Last parameter in startScroll() is duration. Probably your for-loop finishes before the scroller gets anything done :) And also you should call computeScrollOffset() Try this code, it works:

Scroller scroller = new Scroller(getApplicationContext());
scroller.startScroll(0, 0, 100, 100, 500);
while (!scroller.isFinished()) {
    Log.d("scroller", scroller.getCurrX() + " " + scroller.getCurrY());
    scroller.computeScrollOffset();
}
like image 45
agamov Avatar answered Sep 30 '22 12:09

agamov