Add RecyclerView dividers
Add divider decorations to the RecyclerView
This commit is contained in:
@@ -3,7 +3,6 @@ package org.oxycblt.auxio
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -11,7 +10,7 @@ class MainActivity : AppCompatActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
|
||||
Log.d(this::class.simpleName, "Activity Created.")
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import androidx.lifecycle.ViewModelProvider
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.library.recycler.AlbumDataAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
@@ -30,6 +31,7 @@ class LibraryFragment : Fragment() {
|
||||
|
||||
val adapter = AlbumDataAdapter()
|
||||
binding.libraryRecycler.adapter = adapter
|
||||
binding.libraryRecycler.applyDivider()
|
||||
|
||||
libraryModel.albums.observe(
|
||||
viewLifecycleOwner,
|
||||
|
||||
@@ -15,7 +15,7 @@ class AlbumViewHolder(
|
||||
|
||||
if (album.cover == null) {
|
||||
// If there is no cover, clear the ImageView so that the previous
|
||||
// View's cover doesnt stick around.
|
||||
// View's cover doesn't stick around.
|
||||
binding.cover.setImageResource(android.R.color.transparent)
|
||||
} else {
|
||||
binding.cover.setImageBitmap(album.cover)
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package org.oxycblt.auxio.recycler
|
||||
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.ColorUtils
|
||||
import androidx.databinding.BindingAdapter
|
||||
import androidx.recyclerview.widget.DividerItemDecoration
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
|
||||
@@ -13,3 +19,31 @@ fun TextView.numSongsToText(album: Album) {
|
||||
context.getString(R.string.format_multi_song_count, album.numSongs.toString())
|
||||
}
|
||||
}
|
||||
|
||||
// Apply a custom vertical divider
|
||||
fun RecyclerView.applyDivider() {
|
||||
val div = DividerItemDecoration(
|
||||
context,
|
||||
DividerItemDecoration.VERTICAL
|
||||
)
|
||||
|
||||
div.setDrawable(
|
||||
ColorDrawable(
|
||||
getDividerColor(this)
|
||||
)
|
||||
)
|
||||
|
||||
addItemDecoration(div)
|
||||
}
|
||||
|
||||
private fun getDividerColor(recycler: RecyclerView): Int {
|
||||
val isDark = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES
|
||||
|
||||
// Depending on the theme use a different opacity for the divider
|
||||
val alpha = if (isDark) 45 else 85
|
||||
|
||||
return ColorUtils.setAlphaComponent(
|
||||
ContextCompat.getColor(recycler.context, R.color.blue),
|
||||
alpha
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user