[Android] Use ItemDecorators to keep the selection inside the image

Brais Gabín git at videolan.org
Mon Jan 14 16:15:30 CET 2019


vlc-android | branch: master | Brais Gabín <braisgabin at gmail.com> | Sun Jan 13 17:14:32 2019 +0100| [4f9235c25e3c6668f3b5197955b1ae8d1126cc12] | committer: Geoffrey Métais

Use ItemDecorators to keep the selection inside the image

Signed-off-by: Geoffrey Métais <geoffrey.metais at gmail.com>

> https://code.videolan.org/videolan/vlc-android/commit/4f9235c25e3c6668f3b5197955b1ae8d1126cc12
---

 vlc-android/res/layout/video_grid_card.xml         |  4 ----
 .../vlc/gui/helpers/ItemOffsetDecoration.kt        | 25 ++++++++++++++++++++++
 .../videolan/vlc/gui/video/VideoGridFragment.java  | 15 ++++++++++++-
 3 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/vlc-android/res/layout/video_grid_card.xml b/vlc-android/res/layout/video_grid_card.xml
index 2aa64978d..699675080 100644
--- a/vlc-android/res/layout/video_grid_card.xml
+++ b/vlc-android/res/layout/video_grid_card.xml
@@ -57,10 +57,6 @@
         android:longClickable="true"
         android:onClick="@{holder::onClick}"
         android:onLongClick="@{holder::onLongClick}"
-        android:paddingLeft="@dimen/left_right_1610_margin"
-        android:paddingRight="@dimen/left_right_1610_margin"
-        android:paddingBottom="@dimen/top_bottom_1610_margin"
-        android:paddingTop="@dimen/top_bottom_1610_margin"
         android:background="@{bgColor}" >
 
         <!-- Image loading is handled by org.videolan.vlc.gui.helpers.ImageLoader.loadImage() -->
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/ItemOffsetDecoration.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/ItemOffsetDecoration.kt
new file mode 100644
index 000000000..a74a8e97b
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/ItemOffsetDecoration.kt
@@ -0,0 +1,25 @@
+package org.videolan.vlc.gui.helpers
+
+import android.content.res.Resources
+import android.graphics.Rect
+import android.view.View
+
+import androidx.annotation.DimenRes
+import androidx.recyclerview.widget.RecyclerView
+
+// Extracted from https://gist.github.com/yqritc/ccca77dc42f2364777e1
+class ItemOffsetDecoration(
+        private val offsetLeftRight: Int,
+        private val offsetTopBottom: Int
+) : RecyclerView.ItemDecoration() {
+
+    constructor(
+            resources: Resources,
+            @DimenRes offsetLeftRightId: Int,
+            @DimenRes offsetTopBottomId: Int
+    ) : this(resources.getDimensionPixelSize(offsetLeftRightId), resources.getDimensionPixelSize(offsetTopBottomId))
+
+    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
+        outRect.set(offsetLeftRight, offsetTopBottom, offsetLeftRight, offsetTopBottom)
+    }
+}
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 b49c8a639..38bde3c33 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -50,6 +50,7 @@ import org.videolan.vlc.gui.browser.MediaBrowserFragment;
 import org.videolan.vlc.gui.dialogs.ContextSheetKt;
 import org.videolan.vlc.gui.dialogs.CtxActionReceiver;
 import org.videolan.vlc.gui.dialogs.SavePlaylistDialog;
+import org.videolan.vlc.gui.helpers.ItemOffsetDecoration;
 import org.videolan.vlc.gui.helpers.UiTools;
 import org.videolan.vlc.gui.view.SwipeRefreshLayout;
 import org.videolan.vlc.interfaces.IEventsHandler;
@@ -80,6 +81,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
     private VideoGridBinding mBinding;
     private String mGroup;
     private Folder mFolder;
+    private RecyclerView.ItemDecoration mGridItemDecoration;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -160,6 +162,12 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
         outState.putString(Constants.KEY_GROUP, mGroup);
     }
 
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        mGridItemDecoration = null;
+    }
+
     @Override
     public void onChanged(@Nullable List<MediaWrapper> mediaWrappers) {
         mAdapter.showFilename(viewModel.getSort() == Medialibrary.SORT_FILENAME);
@@ -177,6 +185,7 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
             return;
         }
         final Resources res = getResources();
+        if (mGridItemDecoration == null) mGridItemDecoration = new ItemOffsetDecoration(getResources(), R.dimen.left_right_1610_margin, R.dimen.top_bottom_1610_margin);
         final boolean listMode = res.getBoolean(R.bool.list_mode)
                 || (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT &&
                 Settings.INSTANCE.getInstance(requireContext()).getBoolean("force_list_portrait", false));
@@ -185,8 +194,12 @@ public class VideoGridFragment extends MediaBrowserFragment<VideosModel> impleme
         if (!listMode) {
             final int thumbnailWidth = res.getDimensionPixelSize(R.dimen.grid_card_thumb_width);
             final int margin = mBinding.videoGrid.getPaddingStart() + mBinding.videoGrid.getPaddingEnd();
-            mBinding.videoGrid.setColumnWidth(mBinding.videoGrid.getPerfectColumnWidth(thumbnailWidth, margin));
+            final int columnWidth = mBinding.videoGrid.getPerfectColumnWidth(thumbnailWidth, margin) - (res.getDimensionPixelSize(R.dimen.left_right_1610_margin) * 2);
+            mBinding.videoGrid.setColumnWidth(columnWidth);
             mAdapter.setGridCardWidth(mBinding.videoGrid.getColumnWidth());
+            mBinding.videoGrid.addItemDecoration(mGridItemDecoration);
+        } else {
+            mBinding.videoGrid.removeItemDecoration(mGridItemDecoration);
         }
         mBinding.videoGrid.setNumColumns(listMode ? 1 : -1);
         if (mAdapter.isListMode() != listMode) mAdapter.setListMode(listMode);



More information about the Android mailing list