Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class WeeksSelector (context: Context, attributeSet: AttributeSet? = null): LinearLayout(context, attributeSet)
- {
- val data: List<String> = listOf("1", "2", "3", "4", "5", "6", "7", "10", "20", "30", "40")
- val clickItem: Clickable? = null
- private var bgRect = RectF(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat())
- private val paintBG = Paint()
- val roundedRect = 16.dp().toFloat()
- val itemWidth = 40.dp()
- init {
- orientation = HORIZONTAL
- //setBackgroundResource(R.drawable.shape_weeks_selector_bg)
- setData(data)
- setWillNotDraw(false)
- }
- fun setData(list: List<String>)
- {
- var pos = 0
- list.forEach { item ->
- val weeksItem = WeeksItem(context)
- weeksItem.setText(item)
- weeksItem.setOnClickListener {
- clickItem?.let { it.clickItem(pos, item) }
- }
- addView(weeksItem)
- }
- }
- override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
- {
- var width = MeasureSpec.getSize(widthMeasureSpec)
- val height = MeasureSpec.getSize(heightMeasureSpec)
- val leftPadding = paddingLeft
- val rightPadding = paddingRight
- width = data.size * itemWidth + leftPadding + rightPadding
- width = resolveSize(width, widthMeasureSpec)
- setMeasuredDimension(width, height)
- bgRect = RectF(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat())
- }
- override fun onDraw(canvas: Canvas?)
- {
- canvas?.drawRoundRect(bgRect, roundedRect ,roundedRect, paintBG)
- //canvas?.translate(0f, 0f)
- }
- interface Clickable
- {
- fun clickItem(position: Int, text: String)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment