Skip to content

Commit dfe6fbf

Browse files
Lance Johnsonzawadz88
authored andcommitted
Avoid race between updating stepper and ViewPager instantiating the steps.
It was assuming the view pager has layed out all the steps at the time that it calls back to its listeners, but that is not always true (Handler.post() is happening before ViewPager has inserted the step fragments into the fragment manager). This solution waits for the ViewPager to complete its layout before updating StepperLayout.
1 parent 5cffbfe commit dfe6fbf

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

material-stepper/src/main/java/com/stepstone/stepper/StepperLayout.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import android.content.res.TypedArray;
2323
import android.graphics.drawable.Drawable;
2424
import android.os.Build;
25-
import android.os.Handler;
2625
import android.support.annotation.AttrRes;
2726
import android.support.annotation.ColorInt;
2827
import android.support.annotation.DrawableRes;
@@ -40,6 +39,7 @@
4039
import android.view.MotionEvent;
4140
import android.view.View;
4241
import android.view.ViewGroup;
42+
import android.view.ViewTreeObserver;
4343
import android.widget.Button;
4444
import android.widget.LinearLayout;
4545

@@ -310,13 +310,13 @@ public void setAdapter(@NonNull StepAdapter stepAdapter) {
310310
mStepperType.onNewAdapter(stepAdapter);
311311

312312
// this is so that the fragments in the adapter can be created BEFORE the onUpdate() method call
313-
new Handler().post(new Runnable() {
313+
mPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
314314
@Override
315-
public void run() {
315+
public void onGlobalLayout() {
316+
mPager.getViewTreeObserver().removeGlobalOnLayoutListener(this);
316317
onUpdate(mCurrentStepPosition, false);
317318
}
318319
});
319-
320320
}
321321

322322
/**

0 commit comments

Comments
 (0)