Add RecyclerView dividers
Add divider decorations to the RecyclerView
This commit is contained in:
parent
a6e8007972
commit
89398d9f4e
6 changed files with 45 additions and 8 deletions
|
@ -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
|
||||
)
|
||||
}
|
||||
|
|
|
@ -13,12 +13,12 @@
|
|||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="@dimen/padding_small">
|
||||
android:padding="@dimen/padding_medium">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/cover"
|
||||
android:layout_width="@dimen/cover_size"
|
||||
android:layout_height="@dimen/cover_size"
|
||||
android:layout_width="@dimen/cover_size_compact"
|
||||
android:layout_height="@dimen/cover_size_compact"
|
||||
android:contentDescription="@{@string/description_cover_art + album.title}"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
|
@ -32,7 +32,7 @@
|
|||
android:id="@+id/album_name"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/margin_small"
|
||||
android:layout_marginStart="@dimen/margin_medium"
|
||||
android:text="@{album.title}"
|
||||
android:textAppearance="?android:attr/textAppearanceListItem"
|
||||
android:textColor="?android:attr/textColorPrimary"
|
||||
|
@ -46,7 +46,7 @@
|
|||
android:id="@+id/song_count"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/margin_small"
|
||||
android:layout_marginStart="@dimen/margin_medium"
|
||||
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
|
||||
android:textColor="?android:attr/textColorSecondary"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
<resources>
|
||||
<dimen name="padding_tiny">4dp</dimen>
|
||||
<dimen name="padding_small">8dp</dimen>
|
||||
<dimen name="padding_medium">16dp</dimen>
|
||||
|
||||
<dimen name="margin_small">8dp</dimen>
|
||||
<dimen name="margin_medium">16dp</dimen>
|
||||
|
||||
<dimen name="cover_size">64dp</dimen>
|
||||
<dimen name="cover_size_compact">42dp</dimen>
|
||||
|
|
Loading…
Reference in a new issue