-
Notifications
You must be signed in to change notification settings - Fork 13
/
ExpansionPanel.kt
107 lines (85 loc) · 3.23 KB
/
ExpansionPanel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.natura.android.expansionPanel
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import com.natura.android.R
@SuppressLint("CustomViewStyleable")
class ExpansionPanel @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
private val container by lazy { findViewById<CardView>(R.id.ds_expansion_panel_container) }
private val icon by lazy { findViewById<ImageView>(R.id.ds_expansion_panel_icon) }
private val contentArea by lazy { findViewById<ConstraintLayout>(R.id.ds_expansion_panel_content_area) }
private val title by lazy { findViewById<TextView>(R.id.ds_expansion_panel_title) }
private var onStateChangeListener: (isExpanded: Boolean) -> Unit = {}
var isExpanded: Boolean
get() = contentArea.visibility == View.VISIBLE
set(value) {
if (value) {
showContentArea()
} else {
hideContentArea()
}
}
init {
View.inflate(context, R.layout.ds_expansion_panel, this)
setupSubtitle(context, attrs)
setupClickableComponents()
}
fun setOnStateChangeListener(listener: (Boolean) -> Unit) {
onStateChangeListener = listener
}
override fun onFinishInflate() {
super.onFinishInflate()
moveGivenChildrenToContentArea()
}
private fun setupSubtitle(context: Context, attrs: AttributeSet?) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ds_expansion_panel)
val titleText = typedArray.getString(R.styleable.ds_expansion_panel_title)
typedArray.recycle()
title.text = titleText
}
private fun setupClickableComponents() {
container.setOnClickListener {
toggleContentArea()
}
icon.setOnClickListener {
toggleContentArea()
}
}
private fun toggleContentArea() {
if (isExpanded) {
hideContentArea()
} else {
showContentArea()
}
}
private fun showContentArea() {
contentArea.visibility = View.VISIBLE
icon.setImageResource(R.drawable.default_icon_outlined_navigation_arrowtop)
container.setBackgroundResource(R.drawable.ds_expansion_panel_border_expanded)
onStateChangeListener.invoke(true)
}
private fun hideContentArea() {
contentArea.visibility = View.GONE
icon.setImageResource(R.drawable.default_icon_outlined_navigation_arrowbottom)
container.setBackgroundResource(R.drawable.ds_expansion_panel_border_collapsed)
onStateChangeListener.invoke(false)
}
private fun moveGivenChildrenToContentArea() {
while (haveChildrenToMove()) {
val child = getNextChild()
removeView(child)
contentArea.addView(child)
}
}
private fun haveChildrenToMove(): Boolean = getChildAt(1) != null
private fun getNextChild(): View? = getChildAt(1)
}