Skip to content

Commit b36b3f4

Browse files
committed
Change parentLayout and secondLayout to View
1 parent 58cb88a commit b36b3f4

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

expandablelayout/src/main/java/com/skydoves/expandablelayout/ExpandableLayout.kt

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import android.graphics.Color
2424
import android.graphics.drawable.Drawable
2525
import android.util.AttributeSet
2626
import android.view.LayoutInflater
27+
import android.view.View
2728
import android.view.ViewGroup
2829
import android.widget.FrameLayout
2930
import android.widget.RelativeLayout
@@ -37,8 +38,8 @@ import kotlinx.android.synthetic.main.expandable_layout_parent.view.cover
3738
/** An expandable layout that shows a two-level layout with an indicator. */
3839
class ExpandableLayout : FrameLayout {
3940

40-
lateinit var parentLayout: ViewGroup
41-
lateinit var secondLayout: ViewGroup
41+
lateinit var parentLayout: View
42+
lateinit var secondLayout: View
4243
private lateinit var parentFrameLayout: RelativeLayout
4344
@LayoutRes var parentLayoutResource: Int = R.layout.expandable_layout_parent
4445
set(value) {
@@ -125,7 +126,7 @@ class ExpandableLayout : FrameLayout {
125126
a.getResourceId(R.styleable.ExpandableLayout_expandable_secondLayout,
126127
this.secondLayoutResource)
127128
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()
129130
val animation =
130131
a.getInteger(R.styleable.ExpandableLayout_expandable_animation,
131132
this.expandableAnimation.value)
@@ -136,19 +137,19 @@ class ExpandableLayout : FrameLayout {
136137
}
137138
this.spinnerDrawable = a.getDrawable(R.styleable.ExpandableLayout_expandable_spinner)
138139
this.showSpinner =
139-
a.getBoolean(R.styleable.ExpandableLayout_expandable_showSpinner, this.showSpinner)
140+
a.getBoolean(R.styleable.ExpandableLayout_expandable_showSpinner, showSpinner)
140141
this.spinnerAnimate =
141-
a.getBoolean(R.styleable.ExpandableLayout_expandable_spinner_animate, this.spinnerAnimate)
142+
a.getBoolean(R.styleable.ExpandableLayout_expandable_spinner_animate, spinnerAnimate)
142143
this.spinnerRotation =
143-
a.getInt(R.styleable.ExpandableLayout_expandable_spinner_rotation, this.spinnerRotation)
144+
a.getInt(R.styleable.ExpandableLayout_expandable_spinner_rotation, spinnerRotation)
144145
this.spinnerSize =
145-
a.getDimension(R.styleable.ExpandableLayout_expandable_spinner_size, this.spinnerSize)
146+
a.getDimension(R.styleable.ExpandableLayout_expandable_spinner_size, spinnerSize)
146147
this.spinnerMargin =
147-
a.getDimension(R.styleable.ExpandableLayout_expandable_spinner_margin, this.spinnerMargin)
148+
a.getDimension(R.styleable.ExpandableLayout_expandable_spinner_margin, spinnerMargin)
148149
this.spinnerColor =
149-
a.getColor(R.styleable.ExpandableLayout_expandable_spinner_color, this.spinnerColor)
150+
a.getColor(R.styleable.ExpandableLayout_expandable_spinner_color, spinnerColor)
150151
this.isExpanded =
151-
a.getBoolean(R.styleable.ExpandableLayout_expandable_isExpanded, this.isExpanded)
152+
a.getBoolean(R.styleable.ExpandableLayout_expandable_isExpanded, isExpanded)
152153
}
153154

154155
override fun onFinishInflate() {
@@ -169,18 +170,20 @@ class ExpandableLayout : FrameLayout {
169170

170171
private fun updateParentLayout() {
171172
this.parentFrameLayout = inflate(R.layout.expandable_layout_parent) as RelativeLayout
172-
this.parentLayout = inflate(this.parentLayoutResource)
173+
this.parentLayout = inflate(parentLayoutResource)
173174
this.parentLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
174-
this.parentFrameLayout.cover.addView(this.parentLayout)
175+
this.parentFrameLayout.cover.addView(parentLayout)
175176
this.parentFrameLayout.cover.updateLayoutParams { height = parentLayout.measuredHeight }
176-
addView(this.parentFrameLayout)
177+
addView(parentFrameLayout)
177178
}
178179

179180
private fun updateSecondLayout() {
180181
secondLayout = inflate(secondLayoutResource)
182+
secondLayout.visible(false)
181183
addView(secondLayout)
182184
secondLayout.post {
183-
secondLayoutHeight = setMeasureHeight(secondLayout)
185+
secondLayoutHeight = getMeasuredHeight(secondLayout)
186+
secondLayout.visible(true)
184187
with(secondLayout) {
185188
updateLayoutParams { height = 0 }
186189
y = parentLayout.measuredHeight.toFloat()
@@ -208,13 +211,15 @@ class ExpandableLayout : FrameLayout {
208211
}
209212
}
210213

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+
}
218223
}
219224
}
220225
}
@@ -289,15 +294,11 @@ class ExpandableLayout : FrameLayout {
289294
}
290295
}
291296

292-
private fun inflate(@LayoutRes resource: Int): ViewGroup {
297+
private fun inflate(@LayoutRes resource: Int): View {
293298
val inflater: LayoutInflater =
294299
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
295300
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
301302
}
302303

303304
/** Builder class for creating [ExpandableLayout]. */

0 commit comments

Comments
 (0)