Bug 1062859 - Part 1 - Restore scroll position when going back through bookmark folders. r=ahunt

When going down one folder level, we store the current scroll position of the list view and then scroll to the top of the list, so that we always show a child folder starting from the beginning. When navigating back up to its parent, we then restore the previously stored scroll position.

MozReview-Commit-ID: 9C2RMXrlUm1

--HG--
extra : rebase_source : 7ec5a3f1d602db9454efa52603e0f50704c1418d
This commit is contained in:
Jan Henning 2017-01-22 22:02:35 +01:00
parent 9ae155958c
commit de6d526e3c
3 changed files with 42 additions and 14 deletions

View File

@ -62,18 +62,20 @@ class BookmarksListAdapter extends MultiTypeCursorAdapter {
public static class FolderInfo implements Parcelable {
public final int id;
public final String title;
public final int position;
public FolderInfo(int id) {
this(id, "");
this(id, "", 0);
}
public FolderInfo(Parcel in) {
this(in.readInt(), in.readString());
this(in.readInt(), in.readString(), in.readInt());
}
public FolderInfo(int id, String title) {
public FolderInfo(int id, String title, int position) {
this.id = id;
this.title = title;
this.position = position;
}
@Override
@ -85,6 +87,7 @@ class BookmarksListAdapter extends MultiTypeCursorAdapter {
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(title);
dest.writeInt(position);
}
public static final Creator<FolderInfo> CREATOR = new Creator<FolderInfo>() {
@ -104,7 +107,9 @@ class BookmarksListAdapter extends MultiTypeCursorAdapter {
// This is usually implemented by the enclosing fragment/activity.
public static interface OnRefreshFolderListener {
// The folder id to refresh the list with.
public void onRefreshFolder(FolderInfo folderInfo, RefreshType refreshType);
public void onRefreshFolder(FolderInfo folderInfo,
RefreshType refreshType,
int targetPosition);
}
/**
@ -164,9 +169,10 @@ class BookmarksListAdapter extends MultiTypeCursorAdapter {
}
if (mListener != null) {
int targetPosition = mParentStack.peek().position;
// We pick the second folder in the stack as it represents
// the parent folder.
mListener.onRefreshFolder(mParentStack.get(1), RefreshType.PARENT);
mListener.onRefreshFolder(mParentStack.get(1), RefreshType.PARENT, targetPosition);
}
return true;
@ -177,12 +183,14 @@ class BookmarksListAdapter extends MultiTypeCursorAdapter {
*
* @param folderId The id of the folder to show.
* @param folderTitle The title of the folder to show.
* @param position The current position of the list view, which
* will be restored when moving back up one level.
*/
public void moveToChildFolder(int folderId, String folderTitle) {
FolderInfo folderInfo = new FolderInfo(folderId, folderTitle);
public void moveToChildFolder(int folderId, String folderTitle, int position) {
FolderInfo folderInfo = new FolderInfo(folderId, folderTitle, position);
if (mListener != null) {
mListener.onRefreshFolder(folderInfo, RefreshType.CHILD);
mListener.onRefreshFolder(folderInfo, RefreshType.CHILD, 0 /* scroll to top of list */);
}
}

View File

@ -154,7 +154,7 @@ public class BookmarksListView extends HomeListView
// If we're clicking on a folder, update adapter to move to that folder
final int folderId = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
final String folderTitle = adapter.getFolderTitle(parent.getContext(), cursor);
adapter.moveToChildFolder(folderId, folderTitle);
adapter.moveToChildFolder(folderId, folderTitle, getFirstVisiblePosition());
final List<BookmarksListAdapter.FolderInfo> parentStack = ((BookmarksListAdapter) getAdapter()).getParentStack();

View File

@ -54,6 +54,9 @@ public class BookmarksPanel extends HomeFragment {
// Refresh type for folder refreshing loader.
private static final String BOOKMARKS_REFRESH_TYPE = "refresh_type";
// Position that the list view should be scrolled to after loading has finished.
private static final String BOOKMARKS_SCROLL_POSITION = "listview_position";
// List of bookmarks.
private BookmarksListView mList;
@ -137,11 +140,14 @@ public class BookmarksPanel extends HomeFragment {
mListAdapter = new BookmarksListAdapter(activity, null, mSavedParentStack);
mListAdapter.setOnRefreshFolderListener(new OnRefreshFolderListener() {
@Override
public void onRefreshFolder(FolderInfo folderInfo, RefreshType refreshType) {
public void onRefreshFolder(FolderInfo folderInfo,
RefreshType refreshType ,
int targetPosition) {
// Restart the loader with folder as the argument.
Bundle bundle = new Bundle();
bundle.putParcelable(BOOKMARKS_FOLDER_INFO, folderInfo);
bundle.putParcelable(BOOKMARKS_REFRESH_TYPE, refreshType);
bundle.putInt(BOOKMARKS_SCROLL_POSITION, targetPosition);
getLoaderManager().restartLoader(LOADER_ID_BOOKMARKS_LIST, bundle, mLoaderCallbacks);
}
});
@ -207,18 +213,23 @@ public class BookmarksPanel extends HomeFragment {
private static class BookmarksLoader extends SimpleCursorLoader {
private final FolderInfo mFolderInfo;
private final RefreshType mRefreshType;
private final int mTargetPosition;
private final BrowserDB mDB;
public BookmarksLoader(Context context) {
this(context,
new FolderInfo(Bookmarks.FIXED_ROOT_ID, context.getResources().getString(R.string.bookmarks_title)),
RefreshType.CHILD);
new FolderInfo(Bookmarks.FIXED_ROOT_ID, context.getResources().getString(R.string.bookmarks_title), 0),
RefreshType.CHILD, 0);
}
public BookmarksLoader(Context context, FolderInfo folderInfo, RefreshType refreshType) {
public BookmarksLoader(Context context,
FolderInfo folderInfo,
RefreshType refreshType,
int targetPosition) {
super(context);
mFolderInfo = folderInfo;
mRefreshType = refreshType;
mTargetPosition = targetPosition;
mDB = BrowserDB.from(context);
}
@ -267,6 +278,10 @@ public class BookmarksPanel extends HomeFragment {
public RefreshType getRefreshType() {
return mRefreshType;
}
public int getTargetPosition() {
return mTargetPosition;
}
}
/**
@ -280,7 +295,8 @@ public class BookmarksPanel extends HomeFragment {
} else {
FolderInfo folderInfo = (FolderInfo) args.getParcelable(BOOKMARKS_FOLDER_INFO);
RefreshType refreshType = (RefreshType) args.getParcelable(BOOKMARKS_REFRESH_TYPE);
return new BookmarksLoader(getActivity(), folderInfo, refreshType);
final int targetPosition = args.getInt(BOOKMARKS_SCROLL_POSITION);
return new BookmarksLoader(getActivity(), folderInfo, refreshType, targetPosition);
}
}
@ -301,6 +317,10 @@ public class BookmarksPanel extends HomeFragment {
mPanelStateChangeListener.onStateChanged(bundle);
}
if (mList != null) {
mList.setSelection(bl.getTargetPosition());
}
updateUiFromCursor(c);
}