@@ -24,6 +24,7 @@ import android.graphics.Color
24
24
import android.graphics.drawable.Drawable
25
25
import android.util.AttributeSet
26
26
import android.view.LayoutInflater
27
+ import android.view.View
27
28
import android.view.ViewGroup
28
29
import android.widget.FrameLayout
29
30
import android.widget.RelativeLayout
@@ -37,8 +38,8 @@ import kotlinx.android.synthetic.main.expandable_layout_parent.view.cover
37
38
/* * An expandable layout that shows a two-level layout with an indicator. */
38
39
class ExpandableLayout : FrameLayout {
39
40
40
- lateinit var parentLayout: ViewGroup
41
- lateinit var secondLayout: ViewGroup
41
+ lateinit var parentLayout: View
42
+ lateinit var secondLayout: View
42
43
private lateinit var parentFrameLayout: RelativeLayout
43
44
@LayoutRes var parentLayoutResource: Int = R .layout.expandable_layout_parent
44
45
set(value) {
@@ -125,7 +126,7 @@ class ExpandableLayout : FrameLayout {
125
126
a.getResourceId(R .styleable.ExpandableLayout_expandable_secondLayout ,
126
127
this .secondLayoutResource)
127
128
this .duration =
128
- a.getInteger(R .styleable.ExpandableLayout_expandable_duration , this . duration.toInt()).toLong()
129
+ a.getInteger(R .styleable.ExpandableLayout_expandable_duration , duration.toInt()).toLong()
129
130
val animation =
130
131
a.getInteger(R .styleable.ExpandableLayout_expandable_animation ,
131
132
this .expandableAnimation.value)
@@ -136,19 +137,19 @@ class ExpandableLayout : FrameLayout {
136
137
}
137
138
this .spinnerDrawable = a.getDrawable(R .styleable.ExpandableLayout_expandable_spinner )
138
139
this .showSpinner =
139
- a.getBoolean(R .styleable.ExpandableLayout_expandable_showSpinner , this . showSpinner)
140
+ a.getBoolean(R .styleable.ExpandableLayout_expandable_showSpinner , showSpinner)
140
141
this .spinnerAnimate =
141
- a.getBoolean(R .styleable.ExpandableLayout_expandable_spinner_animate , this . spinnerAnimate)
142
+ a.getBoolean(R .styleable.ExpandableLayout_expandable_spinner_animate , spinnerAnimate)
142
143
this .spinnerRotation =
143
- a.getInt(R .styleable.ExpandableLayout_expandable_spinner_rotation , this . spinnerRotation)
144
+ a.getInt(R .styleable.ExpandableLayout_expandable_spinner_rotation , spinnerRotation)
144
145
this .spinnerSize =
145
- a.getDimension(R .styleable.ExpandableLayout_expandable_spinner_size , this . spinnerSize)
146
+ a.getDimension(R .styleable.ExpandableLayout_expandable_spinner_size , spinnerSize)
146
147
this .spinnerMargin =
147
- a.getDimension(R .styleable.ExpandableLayout_expandable_spinner_margin , this . spinnerMargin)
148
+ a.getDimension(R .styleable.ExpandableLayout_expandable_spinner_margin , spinnerMargin)
148
149
this .spinnerColor =
149
- a.getColor(R .styleable.ExpandableLayout_expandable_spinner_color , this . spinnerColor)
150
+ a.getColor(R .styleable.ExpandableLayout_expandable_spinner_color , spinnerColor)
150
151
this .isExpanded =
151
- a.getBoolean(R .styleable.ExpandableLayout_expandable_isExpanded , this . isExpanded)
152
+ a.getBoolean(R .styleable.ExpandableLayout_expandable_isExpanded , isExpanded)
152
153
}
153
154
154
155
override fun onFinishInflate () {
@@ -169,18 +170,20 @@ class ExpandableLayout : FrameLayout {
169
170
170
171
private fun updateParentLayout () {
171
172
this .parentFrameLayout = inflate(R .layout.expandable_layout_parent) as RelativeLayout
172
- this .parentLayout = inflate(this . parentLayoutResource)
173
+ this .parentLayout = inflate(parentLayoutResource)
173
174
this .parentLayout.measure(MeasureSpec .UNSPECIFIED , MeasureSpec .UNSPECIFIED )
174
- this .parentFrameLayout.cover.addView(this . parentLayout)
175
+ this .parentFrameLayout.cover.addView(parentLayout)
175
176
this .parentFrameLayout.cover.updateLayoutParams { height = parentLayout.measuredHeight }
176
- addView(this . parentFrameLayout)
177
+ addView(parentFrameLayout)
177
178
}
178
179
179
180
private fun updateSecondLayout () {
180
181
secondLayout = inflate(secondLayoutResource)
182
+ secondLayout.visible(false )
181
183
addView(secondLayout)
182
184
secondLayout.post {
183
- secondLayoutHeight = setMeasureHeight(secondLayout)
185
+ secondLayoutHeight = getMeasuredHeight(secondLayout)
186
+ secondLayout.visible(true )
184
187
with (secondLayout) {
185
188
updateLayoutParams { height = 0 }
186
189
y = parentLayout.measuredHeight.toFloat()
@@ -208,13 +211,15 @@ class ExpandableLayout : FrameLayout {
208
211
}
209
212
}
210
213
211
- private fun setMeasureHeight (parent : ViewGroup ): Int {
212
- var height = parent.height
213
- for (i in 0 until parent.childCount) {
214
- val child = parent.getChildAt(i)
215
- if (child is ExpandableLayout ) {
216
- child.post {
217
- height + = setMeasureHeight(child)
214
+ private fun getMeasuredHeight (view : View ): Int {
215
+ var height = view.height
216
+ if (view is ViewGroup ) {
217
+ for (i in 0 until view.childCount) {
218
+ val child = view.getChildAt(i)
219
+ if (child is ExpandableLayout ) {
220
+ child.post {
221
+ height + = getMeasuredHeight(child)
222
+ }
218
223
}
219
224
}
220
225
}
@@ -289,15 +294,11 @@ class ExpandableLayout : FrameLayout {
289
294
}
290
295
}
291
296
292
- private fun inflate (@LayoutRes resource : Int ): ViewGroup {
297
+ private fun inflate (@LayoutRes resource : Int ): View {
293
298
val inflater: LayoutInflater =
294
299
context.getSystemService(Context .LAYOUT_INFLATER_SERVICE ) as LayoutInflater
295
300
val view = inflater.inflate(resource, this , false )
296
- if (view is ViewGroup ) {
297
- return view
298
- } else {
299
- throw IllegalArgumentException (" the layout resource should be wrapped a ViewGroup." )
300
- }
301
+ return view
301
302
}
302
303
303
304
/* * Builder class for creating [ExpandableLayout]. */
0 commit comments