Titanium Infinite ScrollView


/ Published in: JavaScript
Save to your folder(s)

based on the NappScrollViewExtended module (it is good but couse my app some problem)


Copy this code and paste it in your HTML
  1. // ScrollView.js
  2.  
  3. function ScrollView(args){
  4. var args = args || {};
  5. var nearBottom = false;
  6. var loadingView = Ti.UI.createView({
  7. height:60,
  8. width: Ti.UI.SIZE,
  9. backgroundColor:"transparent",
  10. visible:false
  11. });
  12. var activityIndicator = Ti.UI.createActivityIndicator({
  13. color: '#000',
  14. font: {fontFamily:'Helvetica Neue', fontSize:16, fontWeight:'bold'},
  15. message: ' Loading...',
  16. style:Ti.UI.iPhone.ActivityIndicatorStyle.DARK,
  17. height:Ti.UI.SIZE,
  18. width:Ti.UI.SIZE
  19. });
  20. activityIndicator.show();
  21. loadingView.add(activityIndicator);
  22.  
  23. var ScrollView = Ti.UI.createScrollView({
  24. width: Ti.UI.SIZE,
  25. height: Ti.UI.FILL,
  26. contentWidth: Ti.UI.SIZE,
  27. contentHeight: Ti.UI.SIZE,
  28. showVerticalScrollIndicator: true,
  29. showHorizontalScrollIndicator: false,
  30. backgroundColor:"transparent",
  31. layout:"vertical"
  32. });
  33. var contentView = Ti.UI.createView({
  34. width: Ti.UI.FILL,
  35. height: Ti.UI.SIZE,
  36. layout: 'vertical',
  37. top:0,
  38. left:0,
  39. backgroundColor:'transparent'
  40. });
  41.  
  42. ScrollView.add(contentView);
  43. ScrollView.add(loadingView);
  44.  
  45. ScrollView.addEventListener('scroll',function(e){
  46. var tolerance = 60;
  47. nearBottom = (contentView.getRect().height - e.y) <= (ScrollView.getRect().height + tolerance);
  48. });
  49.  
  50. ScrollView.addEventListener('scrollend',function(e){
  51. if(nearBottom){
  52. //this.scrollTo(0,bottomLine);
  53. loadingView.setVisible(true);
  54. nearBottom = false;
  55. ScrollView.fireEvent('InfiniteScrolling');
  56. }
  57. });
  58.  
  59. ScrollView.infiniteScrollingCompleted = function(e){
  60. loadingView.setVisible(false);
  61. };
  62.  
  63. ScrollView.addView = function(view){
  64. contentView.add(view);
  65. }
  66.  
  67. return ScrollView;
  68. }
  69. module.exports = ScrollView;
  70.  
  71.  
  72.  
  73. /* SAMPLE USAGE */
  74.  
  75. var win = Ti.UI.createWindow({});
  76.  
  77. var ScrollView = require('ScrollView'); //point to ScrollView.js
  78. scrollView = new ScrollView();
  79.  
  80. // scrollView must be inside a View Container
  81. var contentView = Ti.UI.createView({
  82. width:'90%',
  83. height: 500,
  84. top:200
  85. });
  86. contentView.add(scrollView);
  87. win.add(contentView);
  88.  
  89.  
  90. //Demo infinite load
  91.  
  92. scrollView.addEventListener("InfiniteScrolling", function(e){
  93. Ti.API.info("InfiniteScrolling event -> start loading data");
  94. //simulate download of remote data
  95. setTimeout(function(){
  96. //call infiniteScrollingCompleted when your update is done
  97. scrollView.infiniteScrollingCompleted();
  98.  
  99. //update the scrollView
  100. for(var i=0; i < 3; i++){
  101. var view = Ti.UI.createView({
  102. height:100,
  103. top:10,
  104. backgroundColor:"blue"
  105. });
  106. //i need use addView to add to the subview not add
  107. scrollView.addView(view);
  108. }
  109. },1500);
  110. });
  111.  
  112.  
  113. //demo data
  114. for(var i=0; i < 10; i++){
  115. var view = Ti.UI.createView({
  116. height:100,
  117. top:10,
  118. backgroundColor:"#ccc"
  119. });
  120. //i need use addView to add to the subview not add
  121. scrollView.addView(view);
  122. }
  123.  
  124. win.open();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.