Grid mode option for sources list
parent
1daa02af52
commit
85d09dc48c
@ -0,0 +1,55 @@
|
||||
package org.koitharu.kotatsu.base.ui.util
|
||||
|
||||
import android.view.View
|
||||
import androidx.annotation.Px
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.koitharu.kotatsu.parsers.util.toIntUp
|
||||
import kotlin.math.abs
|
||||
|
||||
class SpanSizeResolver(
|
||||
private val recyclerView: RecyclerView,
|
||||
@Px private val minItemWidth: Int,
|
||||
) : View.OnLayoutChangeListener {
|
||||
|
||||
fun attach() {
|
||||
recyclerView.addOnLayoutChangeListener(this)
|
||||
}
|
||||
|
||||
fun detach() {
|
||||
recyclerView.removeOnLayoutChangeListener(this)
|
||||
}
|
||||
|
||||
override fun onLayoutChange(
|
||||
v: View?,
|
||||
left: Int,
|
||||
top: Int,
|
||||
right: Int,
|
||||
bottom: Int,
|
||||
oldLeft: Int,
|
||||
oldTop: Int,
|
||||
oldRight: Int,
|
||||
oldBottom: Int,
|
||||
) {
|
||||
invalidateInternal(abs(right - left))
|
||||
}
|
||||
|
||||
fun invalidate() {
|
||||
invalidateInternal(recyclerView.width)
|
||||
}
|
||||
|
||||
private fun invalidateInternal(width: Int) {
|
||||
if (width <= 0) {
|
||||
return
|
||||
}
|
||||
val lm = recyclerView.layoutManager as? GridLayoutManager ?: return
|
||||
val estimatedCount = (width / minItemWidth.toFloat()).toIntUp()
|
||||
if (lm.spanCount != estimatedCount) {
|
||||
lm.spanCount = estimatedCount
|
||||
lm.spanSizeLookup?.run {
|
||||
invalidateSpanGroupIndexCache()
|
||||
invalidateSpanIndexCache()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package org.koitharu.kotatsu.explore.ui
|
||||
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
|
||||
import org.koitharu.kotatsu.explore.ui.adapter.ExploreAdapter
|
||||
|
||||
class ExploreGridSpanSizeLookup(
|
||||
private val adapter: ExploreAdapter,
|
||||
private val layoutManager: GridLayoutManager,
|
||||
) : SpanSizeLookup() {
|
||||
|
||||
override fun getSpanSize(position: Int): Int {
|
||||
val itemType = adapter.getItemViewType(position)
|
||||
return if (itemType == ExploreAdapter.ITEM_TYPE_SOURCE_GRID) 1 else layoutManager.spanCount
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/list_selector"
|
||||
android:clipChildren="false"
|
||||
android:gravity="center_horizontal"
|
||||
android:orientation="vertical"
|
||||
android:padding="@dimen/list_spacing"
|
||||
tools:layout_width="120dp">
|
||||
|
||||
<com.google.android.material.imageview.ShapeableImageView
|
||||
android:id="@+id/imageView_icon"
|
||||
android:layout_width="72dp"
|
||||
android:layout_height="72dp"
|
||||
android:background="?colorControlHighlight"
|
||||
android:labelFor="@id/textView_title"
|
||||
android:scaleType="fitCenter"
|
||||
app:shapeAppearance="?shapeAppearanceCornerMedium"
|
||||
tools:src="@tools:sample/avatars" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView_title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/list_spacing"
|
||||
android:elegantTextHeight="false"
|
||||
android:ellipsize="end"
|
||||
android:gravity="center_horizontal"
|
||||
android:singleLine="true"
|
||||
android:textAlignment="center"
|
||||
android:textAppearance="?attr/textAppearanceBodyMedium"
|
||||
tools:text="@tools:sample/lorem[2]" />
|
||||
|
||||
</LinearLayout>
|
||||
Loading…
Reference in New Issue