Given this scenario in jQuery Mobile; two pages with a button on the first page which opens a second page with a list view, I am wanting to prevent the re-initialization of the listview happening every time page two is shown.
<body>
<div data-role="page" data-theme="c" id="page1">
<div data-role="content">
<a id="page1button" href="#page2" data-transition="none" data-role="button">Select an item</a>
</div>
</div>
<div data-role="page" id="page2" data-dom-cache="true">
<div data-role="content">
<ul data-role="listview" data-theme="c" data-filter="true">
<li data-icon="false"><a href="#page1">ABC</a></li>
<li data-icon="false"><a href="#page1">BCD</a></li>
<li data-icon="false"><a href="#page1">CDE</a></li>
<li data-icon="false"><a href="#page1">DEF</a></li>
</ul>
</div>
</div>
</body>
This sample code just shows 4 items in the list view (for the sake of keeping the code short), but my problem can be better illustrated if there are more items in the listview, enough items that will cause the listview to have a scrollbar.
So, for example, on an iPad, if the listview has 80 items, then you will notice that if you are on the second page and you scroll down to the bottom and select an item at the bottom of the list, jQuery Mobile scrolls back to page 1. So far, no problem. But when you click the button to go back to page 2, then the listview is reinitialized and is scrolled to the top again. This is what I am wanting to prevent happening; I am wanting to be able to go to page 1 and then back to page 2 with the listview being in an identical state to how I left it.
So I want the listview initialized only once and not everytime the page is shown. Does anyone know in which event this listview initialization is happening? Is it possible to disable this action?
jQuery Mobile doesn’t preserve scroll position between pages. You should be able to trap the some of the page navigation events and preserve it, though; when you do restore it, you can use
silentScrollto restore the position without triggering many of the events you don’t want to.