@@ -3,15 +3,16 @@ package com.transferwise.sequencelayout
33import android.content.Context
44import android.content.res.TypedArray
55import android.graphics.Rect
6- import androidx.annotation.ColorInt
7- import androidx.annotation.StyleRes
86import android.util.AttributeSet
97import android.view.Gravity
108import android.view.View
119import android.view.ViewGroup
12- import android.view.ViewTreeObserver
1310import android.view.animation.LinearInterpolator
1411import android.widget.FrameLayout
12+ import androidx.annotation.ColorInt
13+ import androidx.annotation.StyleRes
14+ import androidx.core.view.ViewCompat
15+ import androidx.core.view.doOnPreDraw
1516import kotlinx.android.synthetic.main.sequence_layout.view.*
1617
1718/* *
@@ -37,7 +38,7 @@ import kotlinx.android.synthetic.main.sequence_layout.view.*
3738 * @see com.transferwise.sequencelayout.SequenceStep
3839 */
3940public class SequenceLayout (context : Context , attrs : AttributeSet ? , defStyleAttr : Int )
40- : FrameLayout (context, attrs, defStyleAttr), ViewTreeObserver . OnGlobalLayoutListener {
41+ : FrameLayout (context, attrs, defStyleAttr) {
4142
4243 public constructor (context: Context ) : this (context, null )
4344 public constructor (context: Context , attrs: AttributeSet ? ) : this (context, attrs, 0 )
@@ -55,20 +56,13 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr
5556
5657 clipToPadding = false
5758 clipChildren = false
58-
59- start()
6059 }
6160
6261 @ColorInt
6362 private var progressBackgroundColor: Int = 0
6463 @ColorInt
6564 private var progressForegroundColor: Int = 0
6665
67- public fun start () {
68- removeCallbacks(animateToActive)
69- viewTreeObserver.addOnGlobalLayoutListener(this )
70- }
71-
7266 public fun setStyle (@StyleRes defStyleAttr : Int ) {
7367 val attributes = context.theme.obtainStyledAttributes(defStyleAttr, R .styleable.SequenceLayout )
7468 applyAttributes(attributes)
@@ -108,7 +102,7 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr
108102 * Replaces all contained [com.transferwise.sequencelayout.SequenceStep]s with those provided and bound by the adapter
109103 */
110104 public fun <T > setAdapter (adapter : SequenceAdapter <T >) where T : Any {
111- stop( )
105+ removeCallbacks(animateToActive )
112106 removeAllSteps()
113107 val count = adapter.getCount()
114108 for (i in 0 until count) {
@@ -117,7 +111,6 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr
117111 adapter.bindView(view, item)
118112 addView(view)
119113 }
120- start()
121114 }
122115
123116 private fun applyAttributes (attributes : TypedArray ) {
@@ -186,13 +179,12 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr
186179 val scaleEnd = (activeDotTopMargin + (activeDot.measuredHeight / 2 ) - progressBarForegroundTopMargin) /
187180 progressBarBackground.measuredHeight.toFloat()
188181
189- progressBarForeground
190- .animate()
182+ ViewCompat .animate(progressBarForeground)
191183 .setStartDelay(resources.getInteger(R .integer.sequence_step_duration).toLong())
192184 .scaleY(scaleEnd)
193185 .setInterpolator(LinearInterpolator ())
194186 .setDuration(activeStepIndex * resources.getInteger(R .integer.sequence_step_duration).toLong())
195- .setUpdateListener( {
187+ .setUpdateListener {
196188 val animatedOffset = progressBarForeground.scaleY * progressBarBackground.measuredHeight
197189 dotsWrapper
198190 .children()
@@ -212,7 +204,7 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr
212204 }
213205 }
214206 }
215- })
207+ }
216208 .start()
217209 }
218210 }
@@ -224,11 +216,6 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr
224216 return offsetViewBounds.top
225217 }
226218
227- private fun stop () {
228- removeCallbacks(animateToActive)
229- viewTreeObserver.removeOnGlobalLayoutListener(this )
230- }
231-
232219 override fun addView (child : View , index : Int , params : ViewGroup .LayoutParams ) {
233220 if (child is SequenceStep ) {
234221 if (child.isActive()) {
@@ -240,17 +227,14 @@ public class SequenceLayout(context: Context, attrs: AttributeSet?, defStyleAttr
240227 )
241228 }
242229 stepsWrapper.addView(child, params)
230+ child.doOnPreDraw {
231+ setProgressBarHorizontalOffset()
232+ placeDots()
233+ removeCallbacks(animateToActive)
234+ post(animateToActive)
235+ }
243236 return
244237 }
245238 super .addView(child, index, params)
246239 }
247-
248- override fun onGlobalLayout () {
249- if (stepsWrapper.childCount > 0 ) {
250- setProgressBarHorizontalOffset()
251- placeDots()
252- viewTreeObserver.removeOnGlobalLayoutListener(this )
253- post(animateToActive)
254- }
255- }
256240}
0 commit comments