[Android] Video grid: save the position of the first visible item

Adrien Maglo git at videolan.org
Mon Mar 17 17:13:30 CET 2014


vlc-ports/android | branch: master | Adrien Maglo <magsoft at videolan.org> | Mon Mar 17 17:13:22 2014 +0100| [652024480b4d611ef8a0ece563368fdfdd221056] | committer: Adrien Maglo

Video grid: save the position of the first visible item

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=652024480b4d611ef8a0ece563368fdfdd221056
---

 .../videolan/vlc/gui/video/VideoGridFragment.java  |   35 +++++++++++---------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 2ee3874..4e1e2c3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -90,6 +90,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
     private static final int LIST_STRETCH_MODE = GridView.STRETCH_COLUMN_WIDTH;
 
     protected LinearLayout mLayoutFlipperLoading;
+    protected GridView mGridView;
     protected TextView mTextViewNomedia;
     protected Media mItemToUpdate;
     protected String mGroup;
@@ -102,6 +103,9 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
 
     private AudioServiceController mAudioController;
 
+    // Gridview position saved in onPause()
+    private int mGVFirstVisiblePos;
+
     /* All subclasses of Fragment must include a public empty constructor. */
     public VideoGridFragment() { }
 
@@ -131,6 +135,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         // init the information for the scan (1/2)
         mLayoutFlipperLoading = (LinearLayout) v.findViewById(R.id.layout_flipper_loading);
         mTextViewNomedia = (TextView) v.findViewById(R.id.textview_nomedia);
+        mGridView = (GridView) v.findViewById(android.R.id.list);
 
         return v;
     }
@@ -156,6 +161,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
     @Override
     public void onPause() {
         super.onPause();
+        mGVFirstVisiblePos = mGridView.getFirstVisiblePosition();
         mMediaLibrary.removeUpdateHandler(mHandler);
 
         /* Stop the thumbnailer */
@@ -172,6 +178,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         mVideoAdapter.notifyDataSetChanged();
         updateList();
         mMediaLibrary.addUpdateHandler(mHandler);
+        mGridView.setSelection(mGVFirstVisiblePos);
         updateViewMode();
         mAnimator.animate();
 
@@ -200,14 +207,13 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         if (activity == null)
             return true;
 
-        final GridView grid = (GridView)v.findViewById(android.R.id.list);
-
         DisplayMetrics outMetrics = new DisplayMetrics();
         activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
 
         final int itemWidth = Util.convertDpToPx(GRID_ITEM_WIDTH_DP);
         final int horizontalspacing = Util.convertDpToPx(GRID_HORIZONTAL_SPACING_DP);
-        final int width = grid.getPaddingLeft() + grid.getPaddingRight() + horizontalspacing + (itemWidth * 2);
+        final int width = mGridView.getPaddingLeft() + mGridView.getPaddingRight()
+                + horizontalspacing + (itemWidth * 2);
         if (width < outMetrics.widthPixels)
             return true;
         return false;
@@ -219,30 +225,29 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
             return;
         }
 
-        GridView gv = (GridView)getView().findViewById(android.R.id.list);
-
         // Compute the left/right padding dynamically
         DisplayMetrics outMetrics = new DisplayMetrics();
         getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
         int sidePadding = (int) (outMetrics.widthPixels / 100 * Math.pow(outMetrics.density, 3) / 2);
         sidePadding = Math.max(0, Math.min(100, sidePadding));
-        gv.setPadding(sidePadding, gv.getPaddingTop(), sidePadding, gv.getPaddingBottom());
+        mGridView.setPadding(sidePadding, mGridView.getPaddingTop(),
+                sidePadding, mGridView.getPaddingBottom());
 
         // Select between grid or list
         if (hasSpaceForGrid(getView())) {
             Log.d(TAG, "Switching to grid mode");
-            gv.setNumColumns(GridView.AUTO_FIT);
-            gv.setStretchMode(GRID_STRETCH_MODE);
-            gv.setHorizontalSpacing(Util.convertDpToPx(GRID_HORIZONTAL_SPACING_DP));
-            gv.setVerticalSpacing(Util.convertDpToPx(GRID_VERTICAL_SPACING_DP));
-            gv.setColumnWidth(Util.convertDpToPx(GRID_ITEM_WIDTH_DP));
+            mGridView.setNumColumns(GridView.AUTO_FIT);
+            mGridView.setStretchMode(GRID_STRETCH_MODE);
+            mGridView.setHorizontalSpacing(Util.convertDpToPx(GRID_HORIZONTAL_SPACING_DP));
+            mGridView.setVerticalSpacing(Util.convertDpToPx(GRID_VERTICAL_SPACING_DP));
+            mGridView.setColumnWidth(Util.convertDpToPx(GRID_ITEM_WIDTH_DP));
             mVideoAdapter.setListMode(false);
         } else {
             Log.d(TAG, "Switching to list mode");
-            gv.setNumColumns(1);
-            gv.setStretchMode(LIST_STRETCH_MODE);
-            gv.setHorizontalSpacing(LIST_HORIZONTAL_SPACING_DP);
-            gv.setVerticalSpacing(Util.convertDpToPx(LIST_VERTICAL_SPACING_DP));
+            mGridView.setNumColumns(1);
+            mGridView.setStretchMode(LIST_STRETCH_MODE);
+            mGridView.setHorizontalSpacing(LIST_HORIZONTAL_SPACING_DP);
+            mGridView.setVerticalSpacing(Util.convertDpToPx(LIST_VERTICAL_SPACING_DP));
             mVideoAdapter.setListMode(true);
         }
     }



More information about the Android mailing list