OSDN Git Service

Save scrolling position and restore it when going back
authorKirill Rakhman <kirill.rakhman@gmail.com>
Sun, 15 Dec 2013 16:23:09 +0000 (17:23 +0100)
committerKirill Rakhman <kirill.rakhman@gmail.com>
Tue, 17 Dec 2013 15:51:55 +0000 (16:51 +0100)
Patch 1-3: Clean up
Patch 4: Don't scroll in the wrong direction
Patch 5: Remove logging and fix indentation

Change-Id: I5dfa92829a11ce94c4df9bb9891715b48f2b0900

src/com/cyanogenmod/filemanager/parcelables/NavigationViewInfoParcelable.java
src/com/cyanogenmod/filemanager/ui/widgets/NavigationView.java

index 21b7218..fd4983f 100644 (file)
@@ -25,6 +25,7 @@ import com.cyanogenmod.filemanager.model.FileSystemObject;
 import com.cyanogenmod.filemanager.util.FileHelper;
 
 import java.io.File;
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -40,6 +41,7 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
     private boolean mChRooted;
     private List<FileSystemObject> mFiles;
     private List<FileSystemObject> mSelectedFiles;
+    private FileSystemObject mFirstVisible;
 
     /**
      * Constructor of <code>NavigationViewInfoParcelable</code>.
@@ -168,6 +170,24 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
     }
 
     /**
+     * Method that returns the first visible file in the list.
+     *
+     * @return {@link FileSystemObject} The index of the first visible file
+     */
+    public FileSystemObject getFirstVisible() {
+        return mFirstVisible;
+    }
+
+    /**
+     * Method that sets the first visible file.
+     *
+     * @param {@link FileSystemObject} The index of the first visible file
+     */
+    public void setFirstVisible(FileSystemObject firstVisible) {
+        mFirstVisible = firstVisible;
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
@@ -180,25 +200,31 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
      */
     @Override
     public void writeToParcel(Parcel dest, int flags) {
-        //- 0
+        // - 0
         dest.writeInt(this.mId);
-        //- 1
+        // - 1
         dest.writeInt(this.mCurrentDir == null ? 0 : 1);
         if (this.mCurrentDir != null) {
             dest.writeString(this.mCurrentDir);
         }
-        //- 2
+        // - 2
         dest.writeInt(this.mChRooted ? 1 : 0);
-        //- 3
+        // - 3
         dest.writeInt(this.mSelectedFiles == null ? 0 : 1);
         if (this.mSelectedFiles != null) {
             dest.writeList(this.mSelectedFiles);
         }
-        //- 4
+        // - 4
         dest.writeInt(this.mFiles == null ? 0 : 1);
         if (this.mFiles != null) {
             dest.writeList(this.mFiles);
         }
+
+        // - 5
+        dest.writeInt(this.mFirstVisible == null ? 0 : 1);
+        if (this.mFirstVisible != null) {
+            dest.writeSerializable(mFirstVisible);
+        }
     }
 
     /**
@@ -207,32 +233,41 @@ public class NavigationViewInfoParcelable extends HistoryNavigable {
      * @param in The parcel information to recreate the object
      */
     private void readFromParcel(Parcel in) {
-        //- 0
+        // - 0
         this.mId = in.readInt();
-        //- 1
+        // - 1
         int hasCurrentDir = in.readInt();
         if (hasCurrentDir == 1) {
             this.mCurrentDir = in.readString();
         }
-        //- 2
+        // - 2
         this.mChRooted = (in.readInt() == 1);
-        //- 3
+        // - 3
         int hasSelectedFiles = in.readInt();
         if (hasSelectedFiles == 1) {
             List<FileSystemObject> selectedFiles = new ArrayList<FileSystemObject>();
             in.readList(selectedFiles, NavigationViewInfoParcelable.class.getClassLoader());
             this.mSelectedFiles = new ArrayList<FileSystemObject>(selectedFiles);
         }
-        //- 4
+        // - 4
         int hasFiles = in.readInt();
         if (hasFiles == 1) {
             List<FileSystemObject> files = new ArrayList<FileSystemObject>();
             in.readList(files, NavigationViewInfoParcelable.class.getClassLoader());
             this.mFiles = new ArrayList<FileSystemObject>(files);
         }
+
+        // - 5
+        int hasFirstVisible = in.readInt();
+        if (hasFirstVisible == 1) {
+            Serializable readSerializable = in.readSerializable();
+            if (readSerializable instanceof FileSystemObject) {
+                this.mFirstVisible = (FileSystemObject) readSerializable;
+            }
+        }
     }
 
-    /**
+   /**
      * The {@link android.os.Parcelable.Creator}.
      *
      * This field is needed for Android to be able to
index 1563d92..1f1ce3e 100644 (file)
@@ -79,8 +79,8 @@ import java.util.Map;
  * navigate, ...).
  */
 public class NavigationView extends RelativeLayout implements
-    AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener,
-    BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRefreshListener {
+AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener,
+BreadcrumbListener, OnSelectionChangedListener, OnSelectionListener, OnRequestRefreshListener {
 
     private static final String TAG = "NavigationView"; //$NON-NLS-1$
 
@@ -302,29 +302,29 @@ public class NavigationView extends RelativeLayout implements
 
                 //Capture exception (attach task, and use listener to do the anim)
                 ExceptionUtil.attachAsyncTask(
-                    ex,
-                    new AsyncTask<Object, Integer, Boolean>() {
-                        private List<FileSystemObject> mTaskFiles = null;
-                        @Override
-                        @SuppressWarnings({
+                        ex,
+                        new AsyncTask<Object, Integer, Boolean>() {
+                            private List<FileSystemObject> mTaskFiles = null;
+                            @Override
+                            @SuppressWarnings({
                                 "unchecked", "unqualified-field-access"
-                        })
-                        protected Boolean doInBackground(Object... taskParams) {
-                            mTaskFiles = (List<FileSystemObject>)taskParams[0];
-                            return Boolean.TRUE;
-                        }
+                            })
+                            protected Boolean doInBackground(Object... taskParams) {
+                                mTaskFiles = (List<FileSystemObject>)taskParams[0];
+                                return Boolean.TRUE;
+                            }
 
-                        @Override
-                        @SuppressWarnings("unqualified-field-access")
-                        protected void onPostExecute(Boolean result) {
-                            if (!result.booleanValue()) {
-                                return;
+                            @Override
+                            @SuppressWarnings("unqualified-field-access")
+                            protected void onPostExecute(Boolean result) {
+                                if (!result.booleanValue()) {
+                                    return;
+                                }
+                                onPostExecuteTask(
+                                        mTaskFiles, mAddToHistory, mIsNewHistory, mHasChanged,
+                                        mSearchInfo, mNewDirChecked, mScrollTo);
                             }
-                            onPostExecuteTask(
-                                    mTaskFiles, mAddToHistory, mIsNewHistory, mHasChanged,
-                                    mSearchInfo, mNewDirChecked, mScrollTo);
-                        }
-                    });
+                        });
                 final OnRelaunchCommandResult exListener =
                         new OnRelaunchCommandResult() {
                     @Override
@@ -385,15 +385,15 @@ public class NavigationView extends RelativeLayout implements
                 public void run() {
                     Animation fadeAnim = out ?
                             new AlphaAnimation(1, 0) :
-                            new AlphaAnimation(0, 1);
-                    fadeAnim.setDuration(50L);
-                    fadeAnim.setFillAfter(true);
-                    fadeAnim.setInterpolator(new AccelerateInterpolator());
-                    NavigationView.this.startAnimation(fadeAnim);
+                                new AlphaAnimation(0, 1);
+                            fadeAnim.setDuration(50L);
+                            fadeAnim.setFillAfter(true);
+                            fadeAnim.setInterpolator(new AccelerateInterpolator());
+                            NavigationView.this.startAnimation(fadeAnim);
                 }
             });
         }
-   };
+    };
 
     private int mId;
     private String mCurrentDir;
@@ -493,6 +493,14 @@ public class NavigationView extends RelativeLayout implements
         parcel.setChRooted(this.mChRooted);
         parcel.setSelectedFiles(this.mAdapter.getSelectedItems());
         parcel.setFiles(this.mFiles);
+
+        int firstVisiblePosition = mAdapterView.getFirstVisiblePosition();
+        if (firstVisiblePosition >= 0 && firstVisiblePosition < mAdapter.getCount()) {
+            FileSystemObject firstVisible = mAdapter
+                    .getItem(firstVisiblePosition);
+            parcel.setFirstVisible(firstVisible);
+        }
+
         return parcel;
     }
 
@@ -510,8 +518,10 @@ public class NavigationView extends RelativeLayout implements
         this.mFiles = info.getFiles();
         this.mAdapter.setSelectedItems(info.getSelectedFiles());
 
+        final FileSystemObject firstVisible = info.getFirstVisible();
+
         //Update the views
-        refresh();
+        refresh(firstVisible);
         return true;
     }
 
@@ -525,8 +535,8 @@ public class NavigationView extends RelativeLayout implements
         // Retrieve the mode
         this.mNavigationMode = NAVIGATION_MODE.BROWSABLE;
         int mode = tarray.getInteger(
-                                R.styleable.Navigable_navigation,
-                                NAVIGATION_MODE.BROWSABLE.ordinal());
+                R.styleable.Navigable_navigation,
+                NAVIGATION_MODE.BROWSABLE.ordinal());
         if (mode >= 0 && mode < NAVIGATION_MODE.values().length) {
             this.mNavigationMode = NAVIGATION_MODE.values()[mode];
         }
@@ -709,7 +719,7 @@ public class NavigationView extends RelativeLayout implements
             if (this.mAdapterView instanceof FlingerListView) {
                 if (useFlinger) {
                     ((FlingerListView)this.mAdapterView).
-                        setOnItemFlingerListener(this.mOnItemFlingerListener);
+                    setOnItemFlingerListener(this.mOnItemFlingerListener);
                 } else {
                     ((FlingerListView)this.mAdapterView).setOnItemFlingerListener(null);
                 }
@@ -722,17 +732,32 @@ public class NavigationView extends RelativeLayout implements
      *
      * @param fso The file system object
      */
-    public void scrollTo(FileSystemObject fso) {
-        if (fso != null) {
-            try {
-                int position = this.mAdapter.getPosition(fso);
-                this.mAdapterView.setSelection(position);
-            } catch (Exception e) {
-                this.mAdapterView.setSelection(0);
+    public void scrollTo(final FileSystemObject fso) {
+
+        this.mAdapterView.post(new Runnable() {
+
+            @Override
+            public void run() {
+                if (fso != null) {
+                    try {
+                        int position = mAdapter.getPosition(fso);
+                        mAdapterView.setSelection(position);
+
+                        // Make the scrollbar appear
+                        if (position > 0) {
+                            mAdapterView.scrollBy(0, 1);
+                            mAdapterView.scrollBy(0, -1);
+                        }
+
+                    } catch (Exception e) {
+                        mAdapterView.setSelection(0);
+                    }
+                } else {
+                    mAdapterView.setSelection(0);
+                }
             }
-        } else {
-            this.mAdapterView.setSelection(0);
-        }
+        });
+
     }
 
     /**
@@ -801,9 +826,9 @@ public class NavigationView extends RelativeLayout implements
         boolean useFlinger =
                 Preferences.getSharedPreferences().getBoolean(
                         FileManagerSettings.SETTINGS_USE_FLINGER.getId(),
-                            ((Boolean)FileManagerSettings.
-                                    SETTINGS_USE_FLINGER.
-                                        getDefaultValue()).booleanValue());
+                        ((Boolean)FileManagerSettings.
+                                SETTINGS_USE_FLINGER.
+                                getDefaultValue()).booleanValue());
 
         //Creates the new layout
         AdapterView<ListAdapter> newView = null;
@@ -822,7 +847,7 @@ public class NavigationView extends RelativeLayout implements
             if (this.mNavigationMode.compareTo(NAVIGATION_MODE.BROWSABLE) == 0) {
                 if (useFlinger && newView instanceof FlingerListView) {
                     ((FlingerListView)newView).
-                        setOnItemFlingerListener(this.mOnItemFlingerListener);
+                    setOnItemFlingerListener(this.mOnItemFlingerListener);
                 }
             }
 
@@ -835,7 +860,7 @@ public class NavigationView extends RelativeLayout implements
             if (this.mNavigationMode.compareTo(NAVIGATION_MODE.BROWSABLE) == 0) {
                 if (useFlinger && newView instanceof FlingerListView) {
                     ((FlingerListView)newView).
-                        setOnItemFlingerListener(this.mOnItemFlingerListener);
+                    setOnItemFlingerListener(this.mOnItemFlingerListener);
                 }
             }
         }
@@ -1007,13 +1032,6 @@ public class NavigationView extends RelativeLayout implements
                 }
             }
 
-            //Load the data
-            loadData(sortedFiles);
-            this.mFiles = sortedFiles;
-            if (searchInfo != null) {
-                searchInfo.setSuccessNavigation(true);
-            }
-
             //Add to history?
             if (addToHistory && hasChanged && isNewHistory) {
                 if (this.mOnHistoryListener != null) {
@@ -1022,15 +1040,20 @@ public class NavigationView extends RelativeLayout implements
                 }
             }
 
+            //Load the data
+            loadData(sortedFiles);
+            this.mFiles = sortedFiles;
+            if (searchInfo != null) {
+                searchInfo.setSuccessNavigation(true);
+            }
+
             //Change the breadcrumb
             if (this.mBreadcrumb != null) {
                 this.mBreadcrumb.changeBreadcrumbPath(newDir, this.mChRooted);
             }
 
-            //Scroll to object?
-            if (scrollTo != null) {
-                scrollTo(scrollTo);
-            }
+            //If scrollTo is null, the position will be set to 0
+            scrollTo(scrollTo);
 
             //The current directory is now the "newDir"
             this.mCurrentDir = newDir;
@@ -1069,7 +1092,6 @@ public class NavigationView extends RelativeLayout implements
         adapter.clear();
         adapter.addAll(files);
         adapter.notifyDataSetChanged();
-        view.setSelection(0);
     }
 
     /**