DroidFS/app/src/main/java/sushi/hardcore/droidfs/widgets/NonScrollableColoredBorderL...

46 lines
1.5 KiB
Kotlin
Raw Normal View History

2020-07-27 16:20:52 +02:00
package sushi.hardcore.droidfs.widgets
import android.content.Context
2021-11-11 19:46:26 +01:00
import android.graphics.Color
2021-11-09 11:12:09 +01:00
import android.graphics.drawable.ColorDrawable
2020-07-27 16:20:52 +02:00
import android.util.AttributeSet
2021-11-09 11:12:09 +01:00
import android.util.TypedValue
2020-07-27 16:20:52 +02:00
import android.widget.ListAdapter
2021-11-09 11:12:09 +01:00
import android.widget.ListView
import androidx.core.content.ContextCompat
import sushi.hardcore.droidfs.R
2020-07-27 16:20:52 +02:00
2021-11-09 11:12:09 +01:00
class NonScrollableColoredBorderListView: ListView {
constructor(context: Context) : super(context) { applyColor() }
constructor(context: Context, attrs: AttributeSet): super(context, attrs) { applyColor() }
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr) { applyColor() }
2021-11-11 19:46:26 +01:00
val colorAccent: Int
init {
2021-11-09 11:12:09 +01:00
val typedValue = TypedValue()
context.theme.resolveAttribute(R.attr.colorAccent, typedValue, true)
2021-11-11 19:46:26 +01:00
colorAccent = typedValue.data
}
fun applyColor() {
divider = ColorDrawable(colorAccent)
2021-11-09 11:12:09 +01:00
dividerHeight = context.resources.displayMetrics.density.toInt()*2
background = ContextCompat.getDrawable(context, R.drawable.listview_border)
}
2020-08-05 14:06:54 +02:00
fun computeHeight(): Int {
var totalHeight = 0
for (i in 0 until adapter.count){
val item = adapter.getView(i, null, this)
item.measure(0, 0)
totalHeight += item.measuredHeight
}
return totalHeight + (dividerHeight * (adapter.count-1))
}
2020-07-27 16:20:52 +02:00
override fun setAdapter(adapter: ListAdapter?) {
super.setAdapter(adapter)
2020-08-05 14:06:54 +02:00
layoutParams.height = computeHeight()
2020-07-27 16:20:52 +02:00
}
}