Dynamically load table view content in Titanium for Android

2.5k views Asked by At

I am creating an appliction which uses Google Books API. So whenever I search a book it gives a JSON response and I load those results in my table view. There will be thousands of books results when I search. But I don't want to load everything in my tableview. Whenever I scroll down it only has to load next books.

Can anyone give me a code or rough idea on how to do this in Android using Titanium? I have checked this post: https://github.com/appcelerator/KitchenSink/blob/master/Resources/examples/table_view_dynamic_scroll.js But this is for iPhone, I need it for Android as well. Help me out...

1

There are 1 answers

0
Jakub Iwanczuk On

After looking around I have implemented following solution for android:

tableView.addEventListener('scroll', 
    function(e) {
        if (!e.source.__doneUpdating && e.totalItemCount % e.source.__pageSize === 0) {
            var distance = e.totalItemCount - e.firstVisibleItem;
            if (distance <= e.visibleItemCount) {
                if (!e.source.__updating) {
                    e.source.__updating = true;
                    e.source.fireEvent('beginUpdate', e);
                }
            }
        }
        Ti.API.info('-------------------');
        Ti.API.info( 'e.firstVisibleItem: ' + e.firstVisibleItem);
        Ti.API.info( 'e.totalItemCount: ' + e.totalItemCount);
        Ti.API.info( 'e.visibleItemCount: '+ e.visibleItemCount);
    }
);

Where e.source.__pageSize, e.source.__doneUpdating, and e.source.__updating are internal variables that are maintained by the code inserting rows into the tableView.