Automatically Scrolling ScrollableView

You must Login before you can answer or comment on any questions.

Getting to the point, I have a scrollable view. The code is below. I know I can set the scrollview to not take touch events, that much is easy, but what I need to find is how to make it swap views on a timer, say, 5 seconds.

Thanks

var scrollView = Titanium.UI.createScrollableView({
    showPagingControl : true,
});
var viewArray = [];
for(var i = 1; i <= 5; i++) {
    var couponView = Ti.UI.createView({
        top : '55dp',
        width : Ti.Platform.displayCaps.platformWidth,
        height : '150dp',
    });
    var mainButton = Ti.UI.createButton({
        backgroundImage : '/app/images/coupon.png',
        top : '0dp',
        width : '282dp',
        height : '150dp',
    });
    couponView.add(mainButton);
    viewArray.push(couponView);
}
scrollView.views = viewArray;
mainMenu.add(scrollView);

— asked 12 months ago by Jacob Taylor
1 Comment
  • The method likely used will be moveNext();

    However, the problem I'm running into is the timer.

    — commented 12 months ago by Jacob Taylor

3 Answers

Accepted Answer

Ok if you are using iOS you can do scrollingEnabled = false to stop the user from scrolling, on android you might need to overwrite the events.

On the timer do a setInterval to execute the moveNext() function. (this should work for both os)

setInterval(function() {
    sc.moveNext();
            }, 5000);

— answered 12 months ago by Arian Caraballo
answer permalink
1 Comment
  • Unfortunately, moveNext is an Android only event. I actually had to improvise a bit, I can post code. Thanks for the help

    — commented 12 months ago by Jacob Taylor

Solution:

function startScrolling() {
    for(; ; ) {
        sleep(6000);
        Ti.API.info('Scrolling To Index: ' + viewIndex);
        scrollView.scrollToView(viewIndex);
        if(viewIndex == 4) {
            viewIndex = 0;
            scrollView.scrollToView(viewIndex);
        } else {
            scrollView.scrollToView(viewIndex);
            viewIndex++;
        }
    }
}
 
function sleep(milliseconds) {
    var start = new Date().getTime();
    while((new Date().getTime() - start) < milliseconds) {
        // Do nothing
    }
}

— answered 12 months ago by Jacob Taylor
answer permalink
5 Comments
  • try this its a bit cleaner

    setInterval(function() {
        Ti.API.info('Scrolling To Index: ' + viewIndex);
            scrollView.scrollToView(viewIndex);
            if(viewIndex == 4) {
                viewIndex = 0;
                scrollView.scrollToView(viewIndex);
            } else {
                scrollView.scrollToView(viewIndex);
                viewIndex++;
            }
    }, 5000);

    — commented 12 months ago by Arian Caraballo

  • Perfect, I ran into a problem with the loop and not running other threads, and I was just looking into this as you posted. Anyway, thanks for the answer. I was missing a line in my interval, and of course nothing worked XD

    — commented 12 months ago by Jacob Taylor

  • How would I clear it?

    — commented 12 months ago by Jacob Taylor

  • Show 2 more comments

Here is a small example

var count = 0;
var timer = setInterval(function(){
    count++;
    label.text = "count: " + count;
    if (count == 10) {
        clearInterval(timer);//<<<<<<<<---used this to clear the timer
    }
}, 1000);

— answered 12 months ago by Arian Caraballo
answer permalink
1 Comment
  • Just needed it to be a variable I assume, so that I could clear it using clearInterval. Thanks again

    — commented 12 months ago by Jacob Taylor

Your Answer

Think you can help? Login to answer this question!