From c37377bffac15af01e760a670a0985ca4d905d5b Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 24 Jul 2020 22:27:58 -0400 Subject: [PATCH] Remove divider between manga info header and chapters header --- .../tachiyomi/ui/manga/MangaController.kt | 4 +- .../chapter/ChapterDividerItemDecoration.kt | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDividerItemDecoration.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index f597e7175e..5ed61f9bc1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -14,7 +14,6 @@ import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode import androidx.recyclerview.widget.ConcatAdapter -import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.bluelinelabs.conductor.ControllerChangeHandler @@ -47,6 +46,7 @@ import eu.kanade.tachiyomi.ui.library.ChangeMangaCoverDialog import eu.kanade.tachiyomi.ui.library.LibraryController import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.offsetAppbarHeight +import eu.kanade.tachiyomi.ui.manga.chapter.ChapterDividerItemDecoration import eu.kanade.tachiyomi.ui.manga.chapter.ChapterHolder import eu.kanade.tachiyomi.ui.manga.chapter.ChapterItem import eu.kanade.tachiyomi.ui.manga.chapter.ChaptersAdapter @@ -187,7 +187,7 @@ class MangaController : binding.recycler.adapter = ConcatAdapter(mangaInfoAdapter, chaptersHeaderAdapter, chaptersAdapter) binding.recycler.layoutManager = LinearLayoutManager(view.context) - binding.recycler.addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL)) + binding.recycler.addItemDecoration(ChapterDividerItemDecoration(view.context)) binding.recycler.setHasFixedSize(true) chaptersAdapter?.fastScroller = binding.fastScroller diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDividerItemDecoration.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDividerItemDecoration.kt new file mode 100644 index 0000000000..e59a835c2d --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDividerItemDecoration.kt @@ -0,0 +1,60 @@ +package eu.kanade.tachiyomi.ui.manga.chapter + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Rect +import android.graphics.drawable.Drawable +import android.view.View +import androidx.recyclerview.widget.RecyclerView + +/** + * Mimics a DividerItemDecoration that doesn't draw between the first two items. + * + * Used in MangaController since the manga info header and chapters header are the first two + * items in the list using a ConcatAdapter. + */ +class ChapterDividerItemDecoration(context: Context) : RecyclerView.ItemDecoration() { + + private val divider: Drawable + + init { + val a = context.obtainStyledAttributes(intArrayOf(android.R.attr.listDivider)) + divider = a.getDrawable(0)!! + a.recycle() + } + + override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) { + if (parent.layoutManager == null) { + return + } + + canvas.save() + val childCount = parent.childCount + for (i in 1 until childCount) { + val child = parent.getChildAt(i) + val params = child.layoutParams as RecyclerView.LayoutParams + val top = child.bottom + params.bottomMargin + val bottom = top + divider.intrinsicHeight + val left = parent.paddingStart + val right = parent.width - parent.paddingEnd + divider.setBounds(left, top, right, bottom) + divider.draw(canvas) + } + canvas.restore() + } + + override fun getItemOffsets( + outRect: Rect, + view: View, + parent: RecyclerView, + state: RecyclerView.State + ) { + val position = parent.getChildAdapterPosition(view) + + if (position == 0) { + outRect.setEmpty() + } else { + outRect.set(0, 0, 0, divider.intrinsicHeight) + } + } +}