2016-03-07 19:22:56 -05:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.support.v7.widget.RecyclerView
|
|
|
|
import android.util.AttributeSet
|
|
|
|
|
|
|
|
class AutofitRecyclerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
|
|
|
|
RecyclerView(context, attrs) {
|
|
|
|
|
2016-04-15 09:52:53 -04:00
|
|
|
private val manager = NpaGridLayoutManager(context, 1)
|
2016-03-07 19:22:56 -05:00
|
|
|
|
|
|
|
private var columnWidth = -1
|
|
|
|
|
|
|
|
var spanCount = 0
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
if (value > 0) {
|
|
|
|
manager.spanCount = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val itemWidth: Int
|
|
|
|
get() = measuredWidth / manager.spanCount
|
|
|
|
|
|
|
|
init {
|
|
|
|
if (attrs != null) {
|
|
|
|
val attrsArray = intArrayOf(android.R.attr.columnWidth)
|
|
|
|
val array = context.obtainStyledAttributes(attrs, attrsArray)
|
|
|
|
columnWidth = array.getDimensionPixelSize(0, -1)
|
|
|
|
array.recycle()
|
|
|
|
}
|
|
|
|
|
|
|
|
layoutManager = manager
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onMeasure(widthSpec: Int, heightSpec: Int) {
|
|
|
|
super.onMeasure(widthSpec, heightSpec)
|
|
|
|
if (spanCount == 0 && columnWidth > 0) {
|
|
|
|
val spanCount = Math.max(1, measuredWidth / columnWidth)
|
|
|
|
manager.spanCount = spanCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|