/titanium-pull-to-refresh

A JavaScript library providing "Pull To Refresh" functionality for TableViews

Primary LanguageJavaScript

What?

This libray will help you add "pull to refresh" to your TableViews. It is a fork of robedge's code. I fix a number of issues with Titanium 3.x, removed the date.js dependency, translate some text, and restructured the library to allow multiple instances.

How?

To use this method, you will need to include this files in your project:

var PullToRefresh = require("/lib/pulltorefresh");

After include the libraries, you will need to instance a pull-to-refresh variable, like this:

	var pullToRefresh = new PullToRefresh();
var pullToRefreshView = pullToRefresh.createPullToRefreshView({
backgroundColor:"#CCC",
labelColor:"#000",
action: function() {
	setTimeout(function() {
		refresh();
	}, 500);
}

});

You can configure the colors, and in the action, you will put the callback, when the user pull the view (the action). After this, you will add the pull-to-refreshed instanced to your TableView and copy two events (scroll and scrollEnd), like this:

var tableView = Ti.UI.createTableView();

tableView.headerPullView = pullToRefreshView;

tableView.addEventListener("scroll",function(e) { pullToRefresh._scroll(e); });

tableView.addEventListener("dragend",function(e) { pullToRefresh._begin(e, this); });

And, when you finish to show your new data, you will need to keep your TableView at the top, like this:

PullToRefresh._end(function() {
	tableView.setContentInsets({top:0},{animated:true});
});