2018-01-17 18:39:16 +01:00
|
|
|
package jp.juggler.subwaytooter.view
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.graphics.Canvas
|
2018-08-15 12:22:19 +02:00
|
|
|
import android.graphics.Paint
|
2018-01-17 18:39:16 +01:00
|
|
|
import android.graphics.Rect
|
|
|
|
import android.support.v7.widget.RecyclerView
|
|
|
|
import android.view.View
|
|
|
|
import jp.juggler.subwaytooter.R
|
|
|
|
import jp.juggler.subwaytooter.Styler
|
|
|
|
|
|
|
|
class ListDivider(context : Context) : RecyclerView.ItemDecoration() {
|
2018-08-04 20:07:55 +02:00
|
|
|
|
2018-01-17 18:39:16 +01:00
|
|
|
companion object {
|
2018-08-15 12:22:19 +02:00
|
|
|
|
|
|
|
var color :Int =0
|
|
|
|
|
2018-08-04 20:07:55 +02:00
|
|
|
var height : Int = 0
|
2018-08-15 12:22:19 +02:00
|
|
|
|
2018-11-24 08:04:42 +01:00
|
|
|
var marginH : Int = 0
|
2018-01-17 18:39:16 +01:00
|
|
|
}
|
2018-08-04 20:07:55 +02:00
|
|
|
|
2018-08-15 12:22:19 +02:00
|
|
|
private val drawable = Styler.getAttributeDrawable(context, R.attr.colorSettingDivider)
|
|
|
|
private val paint = Paint()
|
|
|
|
private val rect = Rect()
|
2018-01-17 18:39:16 +01:00
|
|
|
|
|
|
|
init {
|
2018-11-24 08:04:42 +01:00
|
|
|
val density = context.resources.displayMetrics.density
|
|
|
|
height = ( density * 1f + 0.5f).toInt()
|
|
|
|
marginH = ( density * 12f + 0.5f).toInt()
|
2018-08-15 12:22:19 +02:00
|
|
|
paint.style = Paint.Style.FILL
|
|
|
|
paint.isAntiAlias = true
|
2018-01-17 18:39:16 +01:00
|
|
|
}
|
|
|
|
|
2018-08-04 20:07:55 +02:00
|
|
|
override fun getItemOffsets(
|
|
|
|
outRect : Rect,
|
|
|
|
view : View,
|
|
|
|
parent : RecyclerView,
|
|
|
|
state : RecyclerView.State
|
|
|
|
) {
|
2018-01-17 18:39:16 +01:00
|
|
|
outRect.set(0, 0, 0, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDraw(canvas : Canvas, parent : RecyclerView, state : RecyclerView.State) {
|
2018-11-24 08:04:42 +01:00
|
|
|
val left = parent.paddingLeft +marginH
|
|
|
|
val right = parent.width - parent.paddingRight -marginH
|
2018-01-17 18:39:16 +01:00
|
|
|
|
2018-08-15 12:22:19 +02:00
|
|
|
if( color != 0){
|
|
|
|
paint.color = color
|
|
|
|
}
|
|
|
|
|
2018-01-17 18:39:16 +01:00
|
|
|
for(i in 0 until parent.childCount) {
|
|
|
|
val child = parent.getChildAt(i)
|
|
|
|
val params = child.layoutParams as RecyclerView.LayoutParams
|
|
|
|
val top = child.bottom + params.bottomMargin
|
|
|
|
val bottom = top + height
|
2018-08-15 12:22:19 +02:00
|
|
|
if( color != 0){
|
|
|
|
rect.set(left, top, right, bottom)
|
|
|
|
canvas.drawRect(rect,paint)
|
|
|
|
}else {
|
|
|
|
drawable.setBounds(left, top, right, bottom)
|
|
|
|
drawable.draw(canvas)
|
|
|
|
}
|
2018-01-17 18:39:16 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-04 20:07:55 +02:00
|
|
|
|
2018-01-17 18:39:16 +01:00
|
|
|
}
|