Guest User

Untitled

a guest
Jan 30th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. class WeeksSelector (context: Context, attributeSet: AttributeSet? = null): LinearLayout(context, attributeSet)
  2. {
  3. val data: List<String> = listOf("1", "2", "3", "4", "5", "6", "7", "10", "20", "30", "40")
  4. val clickItem: Clickable? = null
  5.  
  6. private var bgRect = RectF(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat())
  7. private val paintBG = Paint()
  8. val roundedRect = 16.dp().toFloat()
  9. val itemWidth = 40.dp()
  10.  
  11. init {
  12. orientation = HORIZONTAL
  13. //setBackgroundResource(R.drawable.shape_weeks_selector_bg)
  14. setData(data)
  15. setWillNotDraw(false)
  16. }
  17.  
  18. fun setData(list: List<String>)
  19. {
  20. var pos = 0
  21. list.forEach { item ->
  22. val weeksItem = WeeksItem(context)
  23. weeksItem.setText(item)
  24.  
  25. weeksItem.setOnClickListener {
  26. clickItem?.let { it.clickItem(pos, item) }
  27. }
  28.  
  29. addView(weeksItem)
  30. }
  31. }
  32.  
  33. override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
  34. {
  35. var width = MeasureSpec.getSize(widthMeasureSpec)
  36. val height = MeasureSpec.getSize(heightMeasureSpec)
  37.  
  38. val leftPadding = paddingLeft
  39. val rightPadding = paddingRight
  40.  
  41. width = data.size * itemWidth + leftPadding + rightPadding
  42. width = resolveSize(width, widthMeasureSpec)
  43.  
  44. setMeasuredDimension(width, height)
  45.  
  46. bgRect = RectF(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat())
  47. }
  48.  
  49. override fun onDraw(canvas: Canvas?)
  50. {
  51. canvas?.drawRoundRect(bgRect, roundedRect ,roundedRect, paintBG)
  52.  
  53. //canvas?.translate(0f, 0f)
  54. }
  55.  
  56.  
  57. interface Clickable
  58. {
  59. fun clickItem(position: Int, text: String)
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment