Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-18080] TableView: When removing the window and attaching it again scroll position is lost

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionWon't Fix
Resolution Date2015-02-25T18:31:09.000+0000
Affected Version/sRelease 3.4.0, Release 3.4.1
Fix Version/sn/a
ComponentsAndroid, iOS
Labelsregression, supportTeam
ReporterMarco Cota
AssigneeIngo Muschenetz
Created2014-11-24T17:36:04.000+0000
Updated2017-03-22T23:03:36.000+0000

Description

Issue

In SDK 3.3.0 and higher when a tableView is scroll and the remove from the window and then added again to the window the scroll position is lost and restored to the original starting position, in SDK 3.2.X when doing this the tableView keeps the original position

Steps to repro

1. Run test code 2. Scroll the table 3. Click on a row 4. Click on the "Go Back" button Expected Result: The tableView will be displayed again with the last scroll position Actual Result: The tableView is displayed with the original scroll position (Y value:0)

Test Code

Titanium.UI.setBackgroundColor('#000'); 

var win = Titanium.UI.createWindow({ 
title:'Tab 1', 
backgroundColor:'#fff' 
}); 

var mainView = Ti.UI.createView({ 
top: 45 
}); 

var tableView = Ti.UI.createTableView(); 

var rows = []; 
for (var i = 0; i < 40; i++) { 
rows.push(Ti.UI.createTableViewRow({ 
title: 'Row ' + i 
})); 
}; 

tableView.setData(rows); 

tableView.addEventListener('click', function(e) { 
mainView.add(anotherView); 
mainView.remove(tableView); 
}); 

var anotherView = Ti.UI.createView({ 
layout: 'absolute' 
}); 

var backButton = Ti.UI.createButton({ 
title: 'Go back' 
}); 

backButton.addEventListener('click', function(e) { 
mainView.add(tableView); 
mainView.remove(anotherView); 
}); 

anotherView.add(backButton); 

mainView.add(tableView); 
win.add(mainView); 

win.open(); 

Workaround

In order to keep the las position its possible to use e.firstVisibleItem for android and e.contentOffset for iOS in order to keep a similar effect.
var win = Titanium.UI.createWindow({ 
title:'Tab 1', 
backgroundColor:'#fff' 
}); 

var mainView = Ti.UI.createView({ 
top: 45 
}); 
var tableView = Ti.UI.createTableView(); 

var rows = []; 
for (var i = 0; i < 40; i++) { 
rows.push(Ti.UI.createTableViewRow({ 
title: 'Row ' + i 
})); 
}; 

tableView.setData(rows); 



tableView.addEventListener('click', function(e) { 
mainView.remove(tableView); 
backButton.visible=true; 
}); 



var backButton = Ti.UI.createButton({ 
title: 'Go back', 
top:10, 
visible:false 
}); 
var pos,scroll=false; 

backButton.addEventListener('click', function(e) { 
if(Ti.Platform.osname == 'android'){ 
tableView.scrollToIndex(pos); 
}else{ 
tableView.scrollToTop(pos.y,false); 
} 
mainView.add(tableView); 

backButton.visible=false; 
}); 


tableView.addEventListener('scroll',function(e){ 
if(Ti.Platform.osname == 'android'){ 
pos = e.firstVisibleItem; 
}else{ 
pos = e.contentOffset; 
} 
Ti.API.info(e.firstVisibleItem); 
}); 

win.add(backButton); 

mainView.add(tableView); 
win.add(mainView); 

win.open();

Comments

  1. Vishal Duggal 2015-02-25

    contentOffset and firstVisible item are not supported properties of the TableView on the Titanium platform. When a window is closed or the view is removed from the parent, the view is destroyed and recreated when the window is opened again or the view is reattached to the parent. When it is recreated only properties that are supported by the proxy are applied to the view. Hence the scroll position is lost.
  2. Lee Morris 2017-03-22

    Closing ticket as "Won't Fix".

JSON Source