Changed media browser to save ListView state (including scroll position).

This commit is contained in:
Felix Ableitner 2013-09-19 23:15:26 +02:00
parent 2926d3af2f
commit b9802245fe

View file

@ -50,6 +50,7 @@ import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
@ -92,6 +93,11 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
*/
private Stack<String> mCurrentPath = new Stack<String>();
/**
* Holds the scroll position in the list view at each directory level.
*/
private Stack<Parcelable> mListState = new Stack<Parcelable>();
/**
* Cling UPNP service.
*/
@ -176,14 +182,15 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
* Opens a new directory and displays it.
*/
private void getFiles(String directory) {
mListState.push(getListView().onSaveInstanceState());
mCurrentPath.push(directory);
getFiles();
getFiles(false);
}
/**
* Displays the current directory on the ListView.
*/
private void getFiles() {
private void getFiles(final boolean restoreListState) {
Service<?, ?> service = mCurrentServer.findService(
new ServiceType("schemas-upnp-org", "ContentDirectory"));
mUpnpService.getControlPoint().execute(new Browse(service,
@ -202,6 +209,10 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
mFileAdapter.add(c);
for (Item i : didl.getItems())
mFileAdapter.add(i);
if (restoreListState)
getListView().onRestoreInstanceState(mListState.pop());
else
getListView().setSelectionFromTop(0, 0);
}
});
}
@ -228,13 +239,15 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
public boolean onBackPressed() {
if (getListAdapter() == mServerAdapter)
return false;
mCurrentPath.pop();
if (mCurrentPath.empty()) {
setListAdapter(mServerAdapter);
getListView().onRestoreInstanceState(mListState.pop());
mCurrentServer = null;
}
else {
getFiles();
getFiles(true);
}
return true;
}