From 823309c35e4c7a5bfc4ea64db02857375b0fa017 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 17 Jan 2021 23:07:26 +0100 Subject: [PATCH] change the way grid spacing is handled, make it more dynamic --- .../gallery/pro/activities/MediaActivity.kt | 15 +++++++++++ .../pro/helpers/GridSpacingItemDecoration.kt | 27 +++++++++++++++++++ .../main/res/layout/photo_video_item_grid.xml | 3 +-- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/GridSpacingItemDecoration.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt index d136ee640..2a773ec0a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt @@ -405,6 +405,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } setupLayoutManager() measureRecyclerViewContent(mMedia) + handleGridSpacing() } else if (mLastSearchedText.isEmpty()) { (currAdapter as MediaAdapter).updateMedia(mMedia) measureRecyclerViewContent(mMedia) @@ -723,6 +724,19 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { media_vertical_fastscroller.setScrollToY(media_grid.computeVerticalScrollOffset()) } + private fun handleGridSpacing() { + if (media_grid.itemDecorationCount > 0) { + media_grid.removeItemDecorationAt(0) + } + + val viewType = config.getFolderViewType(if (mShowAll) SHOW_ALL else mPath) + if (viewType == VIEW_TYPE_GRID) { + val spanCount = config.mediaColumnCnt + val spacing = 2 + media_grid.addItemDecoration(GridSpacingItemDecoration(spanCount, spacing, config.scrollHorizontally)) + } + } + private fun initZoomListener() { val viewType = config.getFolderViewType(if (mShowAll) SHOW_ALL else mPath) if (viewType == VIEW_TYPE_GRID) { @@ -773,6 +787,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } private fun columnCountChanged() { + handleGridSpacing() invalidateOptionsMenu() getMediaAdapter()?.apply { notifyItemRangeChanged(0, media.size) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/GridSpacingItemDecoration.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/GridSpacingItemDecoration.kt new file mode 100644 index 000000000..07aa1cc43 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/GridSpacingItemDecoration.kt @@ -0,0 +1,27 @@ +package com.simplemobiletools.gallery.pro.helpers + +import android.graphics.Rect +import android.view.View +import androidx.recyclerview.widget.RecyclerView + +class GridSpacingItemDecoration(val spanCount: Int, val spacing: Int, val isScrollingHorizontally: Boolean) : RecyclerView.ItemDecoration() { + + override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { + val position = parent.getChildAdapterPosition(view) + val column = position % spanCount + + if (isScrollingHorizontally) { + outRect.top = column * spacing / spanCount + outRect.bottom = spacing - (column + 1) * spacing / spanCount + if (position >= spanCount) { + outRect.left = spacing + } + } else { + outRect.left = column * spacing / spanCount + outRect.right = spacing - (column + 1) * spacing / spanCount + if (position >= spanCount) { + outRect.top = spacing + } + } + } +} diff --git a/app/src/main/res/layout/photo_video_item_grid.xml b/app/src/main/res/layout/photo_video_item_grid.xml index 64dbbf88f..cc3fc718d 100644 --- a/app/src/main/res/layout/photo_video_item_grid.xml +++ b/app/src/main/res/layout/photo_video_item_grid.xml @@ -5,8 +5,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" - android:focusable="true" - android:padding="1px"> + android:focusable="true">