mirror of
https://github.com/skyline-emu/skyline.git
synced 2024-11-04 23:55:08 +01:00
Create an ItemDecorations
file for all RecyclerView
item decorations
All item decorations are now placed in one file so that any `RecyclerView` in the app can use the same ones.
This commit is contained in:
parent
a59f2baa3a
commit
1dfea9ef6f
@ -7,7 +7,6 @@ package emu.skyline
|
||||
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.graphics.Rect
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.provider.DocumentsContract
|
||||
@ -154,39 +153,13 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private inner class GridSpacingItemDecoration : RecyclerView.ItemDecoration() {
|
||||
private val padding = resources.getDimensionPixelSize(R.dimen.grid_padding)
|
||||
|
||||
override fun getItemOffsets(outRect : Rect, view : View, parent : RecyclerView, state : RecyclerView.State) {
|
||||
super.getItemOffsets(outRect, view, parent, state)
|
||||
|
||||
val gridLayoutManager = parent.layoutManager as GridLayoutManager
|
||||
val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams
|
||||
when (layoutParams.spanIndex) {
|
||||
0 -> outRect.left = padding
|
||||
|
||||
gridLayoutManager.spanCount - 1 -> outRect.right = padding
|
||||
|
||||
else -> {
|
||||
outRect.left = padding / 2
|
||||
outRect.right = padding / 2
|
||||
}
|
||||
}
|
||||
|
||||
if (layoutParams.spanSize == gridLayoutManager.spanCount) {
|
||||
outRect.left = 0
|
||||
outRect.right = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setAppListDecoration() {
|
||||
binding.appList.apply {
|
||||
while (itemDecorationCount > 0) removeItemDecorationAt(0)
|
||||
when (layoutType) {
|
||||
LayoutType.List -> Unit
|
||||
|
||||
LayoutType.Grid, LayoutType.GridCompact -> addItemDecoration(GridSpacingItemDecoration())
|
||||
LayoutType.Grid, LayoutType.GridCompact -> addItemDecoration(GridSpacingItemDecoration(resources.getDimensionPixelSize(R.dimen.grid_padding)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
48
app/src/main/java/emu/skyline/adapter/ItemDecorations.kt
Normal file
48
app/src/main/java/emu/skyline/adapter/ItemDecorations.kt
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
* Copyright © 2022 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
||||
*/
|
||||
|
||||
package emu.skyline.adapter
|
||||
|
||||
import android.graphics.Rect
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
/**
|
||||
* A [RecyclerView.ItemDecoration] that adds a margin at the bottom of each item
|
||||
*/
|
||||
class SpacingItemDecoration(private val padding : Int) : RecyclerView.ItemDecoration() {
|
||||
override fun getItemOffsets(outRect : Rect, view : View, parent : RecyclerView, state : RecyclerView.State) {
|
||||
super.getItemOffsets(outRect, view, parent, state)
|
||||
outRect.set(0, 0, 0, padding)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A [RecyclerView.ItemDecoration] that adds a margin to the sides of each item in a grid
|
||||
*/
|
||||
class GridSpacingItemDecoration(private val padding : Int) : RecyclerView.ItemDecoration() {
|
||||
override fun getItemOffsets(outRect : Rect, view : View, parent : RecyclerView, state : RecyclerView.State) {
|
||||
super.getItemOffsets(outRect, view, parent, state)
|
||||
|
||||
val gridLayoutManager = parent.layoutManager as GridLayoutManager
|
||||
val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams
|
||||
when (layoutParams.spanIndex) {
|
||||
0 -> outRect.left = padding
|
||||
|
||||
gridLayoutManager.spanCount - 1 -> outRect.right = padding
|
||||
|
||||
else -> {
|
||||
outRect.left = padding / 2
|
||||
outRect.right = padding / 2
|
||||
}
|
||||
}
|
||||
|
||||
if (layoutParams.spanSize == gridLayoutManager.spanCount) {
|
||||
outRect.left = 0
|
||||
outRect.right = 0
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user