Pull to Refresh stopped working in Titanium Mobile SDK 2.1.0

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

Hi folks,

Anyone else had issues with Pull to refresh not working on the latest Titanium SDK?

If not, anyone know why this may not work?

It's been a while, and I can't remember if the code below came from Kitchen Sink or from a tutorial elsewhere (it wasn't written by me)

// Pull to Refresh Function
function formatDate() {
    var date = new Date();
    // month naming array
    var month = new Array(12);
    month[0] = "01";
    month[1] = "02";
    month[2] = "03";
    month[3] = "04";
    month[4] = "05";
    month[5] = "06";
    month[6] = "07";
    month[7] = "08";
    month[8] = "09";
    month[9] = "10";
    month[10] = "11";
    month[11] = "12";
 
    //document.write("The current month is " + month[d.getMonth()]);
    var datestr = date.getDate() + '/' + month[date.getMonth()] + '/' + date.getFullYear();
    if (date.getHours() >= 12) {
        datestr += ' ' + (date.getHours() == 12 ? date.getHours() : date.getHours()) + ':' + date.getMinutes();
        //datestr+=' '+(date.getHours()==12 ? date.getHours() : date.getHours()-12)+':'+date.getMinutes()+' PM';
    } else {
        datestr += ' ' + date.getHours() + ':' + date.getMinutes();
    }
    return datestr;
}
 
var lastRow = 4;
 
var border = Ti.UI.createView({
    backgroundColor: "#576c89",
    height: 2,
    bottom: 0
});
 
var tableHeader = Ti.UI.createView({
    backgroundColor: "#bcbdc1",
    width: 320,
    height: 60
});
 
// fake it til ya make it..  create a 2 pixel
// bottom border
tableHeader.add(border);
 
var arrow = Ti.UI.createView({
    backgroundImage: "images/pulltorefresh_arrow.png",
    width: 23,
    height: 60,
    bottom: 10,
    left: 20
});
 
var statusLabel = Ti.UI.createLabel({
    text: "Pull to Reload",
    left: 55,
    width: 200,
    bottom: 30,
    height: "auto",
    color: "#FFF",
    textAlign: "center",
    font: {
        fontSize: 13,
        fontWeight: "bold"
    },
    shadowColor: "#999",
    shadowOffset: {
        x: 0,
        y: 1
    }
});
 
var lastUpdatedLabel = Ti.UI.createLabel({
    text: "Last Updated: " + formatDate(),
    left: 55,
    width: 200,
    bottom: 15,
    height: "auto",
    color: "#717276",
    textAlign: "center",
    font: {
        fontSize: 12
    },
    shadowColor: "#999",
    shadowOffset: {
        x: 0,
        y: 1
    }
});
 
var actInd = Titanium.UI.createActivityIndicator({
    left: 20,
    bottom: 13,
    width: 30,
    height: 30
});
 
tableHeader.add(arrow);
tableHeader.add(statusLabel);
tableHeader.add(lastUpdatedLabel);
tableHeader.add(actInd);
 
tableview.headerPullView = tableHeader;
 
 
var pulling = false;
var reloading = false;
 
function beginReloading() {
    // just mock out the reload
    setTimeout(endReloading, 2000);
}
 
function endReloading() {
    // simulate loading
    for (var c = lastRow; c < lastRow + 10; c++) {
        tableview.appendRow({
            title: "Row " + c
        });
    }
    lastRow += 10;
 
    // when you're done, just reset
    tableview.setContentInsets({
        top: 0
    }, {
        animated: true
    });
    reloading = false;
    lastUpdatedLabel.text = "Last Updated: " + formatDate();
    statusLabel.text = "Pull down to refresh...";
    actInd.hide();
    arrow.show();
    // clear data
    tableview.setData();
    // remove lines?
    tableview.separatorColor = 'transparent';
 
 
    // this bit below reloads the tweets
    loadNews();
}
 
tableview.addEventListener('scroll', function (e) {
    var offset = e.contentOffset.y;
    if (offset <= -65.0 && !pulling) {
        var t = Ti.UI.create2DMatrix();
        t = t.rotate(-180);
        pulling = true;
        arrow.animate({
            transform: t,
            duration: 180
        });
        statusLabel.text = "Release to refresh...";
    } else if (pulling && offset > -65.0 && offset < 0) {
        pulling = false;
        var t = Ti.UI.create2DMatrix();
        arrow.animate({
            transform: t,
            duration: 180
        });
        statusLabel.text = "Pull down to refresh...";
    }
});
 
tableview.addEventListener('scrollEnd', function (e) {
    if (pulling && !reloading && e.contentOffset.y <= -65.0) {
        reloading = true;
        pulling = false;
        arrow.hide();
        actInd.show();
        statusLabel.text = "Reloading...";
        tableview.setContentInsets({
            top: 60
        }, {
            animated: true
        });
        arrow.transform = Ti.UI.create2DMatrix();
        beginReloading();
    }
});
Simon

— asked 12 months ago by Simon Hume
0 Comments

2 Answers

Accepted Answer

It's funny to see this on here already. I just finished up fixing this code on one of my apps that I was upgrading to 2.1. To fix, pull the contentOffset value out of the 'scroll' event and change the 'scrollEnd' event to 'dragEnd'. Apparently the 'scroll' event now continues to fire even after the 'scrollEnd' event, which is causing the issue.

Just a quick snippet:

var offset = 0;
tableview.addEventListener('scroll', function (e) {
    offset = e.contentOffset.y;
and
tableview.addEventListener('dragEnd', function (e) {
    if (pulling && !reloading && offset <= -65.0) {

— answered 12 months ago by Matt Berg
answer permalink
10 Comments
  • Matt, superb, that's fixed it. Thanks for taking the time to share your findings! All the best, Simon

    — commented 12 months ago by Simon Hume

  • no problem, literally just fixed the issue like 1 hour before. noticed the same strange behavior.

    — commented 12 months ago by Matt Berg

  • Thanks Matt! The dragEnd should be inside API documentation.

    — commented 11 months ago by Aizu Ikmal Ahmad

  • Show 7 more comments

Hi All, For anyone else facing this same challenge: Here is how I selved it:

1. Look for the following code

//create the table view
var tblRecipes = Titanium.UI.createTableView({
    height: 366,
     width: 320,
     top: 0,
     left: 0,
     rowHeight: 70,
     search: searchBar,
     //Here is the filter attribute that appears in Tabe View
     filterAttribute: 'filter' 
});
If the above code is repeating it self then comment out the 2nd time that you see it. My file had this code 2 times. I deleted the 2nd time I had it in my file and that worked. Please do keep

win.add(tblRecipes);

from the 2nd code.

Your Answer

Think you can help? Login to answer this question!