5

Background

I'm trying to make an AdapterView (listview, gridview,...) to slowly auto scroll .

the user can toggle it whenever he wishes, and it doesn't necessary move all the way to the end of the adapterView.

again, i DO NOT wish to make the adapterView to scroll all the way to the bottom since the user should be able to cancel the auto-scrolling.

What I've tried

for this, i'm using the next code:

private static final SCROLLING_RUNNABLE = new Runnable() {

    @Override
    public void run() {
        final int duration = 10;
        final int pixelsToMove = 1;
        mAdapterView.smoothScrollBy(pixelsToMove, duration);
        mHandler.postDelayed(this, duration);
    }
};

...
// use this to turn auto-scrolling on:
mHandler.post(SCROLLING_RUNNABLE);
// use this to turn auto-scrolling off:
mHandler.removeCallbacks(SCROLLING_RUNNABLE);

The problem

the code usually works, but on some cases, it takes a long time till the scrolling starts.

in fact, it seems that the more i turn it on and off, the more i will have to wait for it to start auto-scrolling.

i think this issue occurs only if i scroll back to the top. it works just fine when the adapterView is in scrolled a bit.

since i can't find out how to get the current scrolling value of the adapterView, i can't find out how to fix this issue.

The question

What can I do in order to fix this issue ? Is there an alternative?

android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

1

You can start scrolling SCROLLING_RUNNABLE.run();

UPDATE Or you can use animations:

Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                super.applyTransformation(interpolatedTime, t);
                // scroll the list
            }
        };

and clearAnimation() for stopping.

getKonstantin
  • 1,220
  • 9
  • 14
  • no, the handler works just fine, since if i put logs there, i can see they get printed. about animation, i don't see how this could help... – android developer Sep 09 '13 at 12:25
  • Try replace `mHandler.post(SCROLLING_RUNNABLE);` with `mHandler. postAtFrontOfQueue(SCROLLING_RUNNABLE);` – getKonstantin Sep 09 '13 at 12:32
  • again, the handler works fine as the logs are printed if i put there logs. it's the scrolling that doesn't work well. however, now that i've tested it (and the original idea of just calling run() ), i see that it works fine. what is going on? – android developer Sep 09 '13 at 13:11