Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Help with Pull to Refresh for RSS Feed

Hello, I am looking for some help on adding the 'pull to refresh' code (found here: http://developer.appcelerator.com/blog/2010/05/how-to-create-a-tweetie-like-pull-to-refresh-table.html and in the Kitchen Sink). The code I have for my RSS Feed is pasted at this link: (it's gets a pretty sexy RSS feed with "prettydates" displayed nicely.)

http://pastie.org/1320085

I tried adding this for the Pull to Refresh code:

function formatDate()
{
    var date = new Date;
    var datestr = date.getMonth()+'/'+date.getDate()+'/'+date.getFullYear();
    if (date.getHours()>=12)
    {
        datestr+=' '+(date.getHours()==12 ? date.getHours() : date.getHours()-12)+':'+date.getMinutes()+' PM';
    }
    else
    {
        datestr+=' '+date.getHours()+':'+date.getMinutes()+' AM';
    }
    return datestr;
}

var border = Ti.UI.createView({
    backgroundColor:"#576c89",
    height:2,
    bottom:0
})

var tableHeader = Ti.UI.createView({
    backgroundColor:"#e2e7ed",
    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/whiteArrow.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:"#576c89",
    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:"#576c89",
    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()
{
    // This is where I get lost
    for (var c=0;c<items.length;c++)
    {
        tableView.appendRow();
        // ????
    }


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

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

Any suggestions would be greatly appreciated!!

— asked November 23rd 2010 by Erick Arbe
  • feed
  • help
  • iphone
  • pull
  • refresh
  • rss
  • to
0 Comments

8 Answers

  • This may not help, but I've made a Tweetie-like pull to refresh for Android. It requires set heights, but it works. This should also be able to be used for iPhone, see if you can use it for your project.

    http://gist.github.com/903895

    — answered April 5th 2011 by Joe iEntry
    permalink
    1 Comment
    • great thing this piece of code. exactly what i've been searching for. thanks for that.

      — commented September 19th 2011 by Sebastian Klaus
  • You might want to look at this
    http://developer.appcelerator.com/blog/2010/05/how-to-create-a-tweetie-like-pull-to-refresh-table.html

    — answered November 23rd 2010 by Andreas Kviby
    permalink
    1 Comment
    • Um, that's the same link I posted above. But thanks for reading the question.

      — commented November 23rd 2010 by Erick Arbe
  • Were you ever able to resolve this? I'm pulling in json data and displaying it similar to the twitter example but wanted to use the pull down to refresh. I'm stumped at the same place you were. I'm guessing that my initial data call should be replicated using

    
    function endReloading()
    {
    
            //simulate loading
            for (var i = widgets; i < widgets; i++)
        {
            tableView.setData(rowData);
        }
            widgets += 20;
    

    It's not kicking any errors but its not working either.

    — answered December 22nd 2010 by Forrest Frazier
    permalink
    0 Comments
  • @Forrest - is the tableView's scope correct? It should work.

    — answered December 22nd 2010 by Dan Tamas
    permalink
    1 Comment
    • I believe so. Initially when I create my table view (which does work) I set my data to "rowData" so I thought on reload I should keep it set to "rowData" or am I missing something?

      Here is my full code.
      http://pastie.org/1397755

      — commented December 22nd 2010 by Forrest Frazier
  • I think setdata is not triggering for (var i = widgets; i < widgets; i++)

    
    

    function endReloading()
    {

        //simulate loading
    //for (var c=lastRow;c<lastRow+10;c++)
        for (var i = widgets; i < widgets; i++)
    {
         // tableView.appendRow({title:"Row "+c});
        tableView.setData(rowData);
    }
    
    — answered December 22nd 2010 by Dan Tamas
    permalink
    1 Comment
    • I would agree. I added an alert box and it never popped.

      function beginReloading()
      {
          // just mock out the reload
          setTimeout(endReloading,2000);
      }
      
      function endReloading()
      {
      
              //simulate loading
              for (var i = widgets; i < widgets; i++)
          {
      alert("reload was attempted");
              // tableView.setData(rowData);
          }
              widgets += 20;
      
          // 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();
      }
      

      So there must be something wrong with my for (var i = widgets; i &lt; widgets; i++) line. I've also tried for (var i = 0; i &lt; widgets.length; i++) that also fails.

      I'm sure this is something simple but I just cant see it.

      — commented December 22nd 2010 by Forrest Frazier
  • pull to refresh example from kitchen sink is buggy on SDK 1.6.0
    see http://developer.appcelerator.com/question/113991/bug-scrollend-event-is-not-returning-negative-contentoffsety-titanium-sdk-160

    — answered February 19th 2011 by gondo gondo
    permalink
    1 Comment
    • also note that setContentInsets is undocumented() function what is not supported on SDK 1.6.0 use scrollToTop() instead

      — commented February 19th 2011 by gondo gondo
  • have you ever managed to get it to work?

    — answered December 8th 2011 by Roy Wijkstra
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.