Skip to content

Commit 6178e71

Browse files
melaniegoetzdsn5ft
authored andcommitted
Update BottomSheet to have a singular SettleRunnable
Resolves #516 PiperOrigin-RevId: 283802793 (cherry picked from commit e057829)
1 parent 996a484 commit 6178e71

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ public abstract static class BottomSheetCallback {
207207

208208
private boolean isShapeExpanded;
209209

210+
private SettleRunnable settleRunnable = null;
211+
210212
@Nullable private ValueAnimator interpolatorAnimator;
211213

212214
private static final int DEF_STYLE_RES = R.style.Widget_Design_BottomSheet_Modal;
@@ -1181,7 +1183,19 @@ void startSettlingAnimation(View child, int state, int top, boolean settleFromVi
11811183
setStateInternal(STATE_SETTLING);
11821184
// STATE_SETTLING won't animate the material shape, so do that here with the target state.
11831185
updateDrawableForTargetState(state);
1184-
ViewCompat.postOnAnimation(child, new SettleRunnable(child, state));
1186+
if (settleRunnable == null) {
1187+
// If the singleton SettleRunnable instance has not been instantiated, create it.
1188+
settleRunnable = new SettleRunnable(child, state);
1189+
}
1190+
// If the SettleRunnable has not been posted, post it with the correct state.
1191+
if (settleRunnable.isPosted == false) {
1192+
settleRunnable.targetState = state;
1193+
ViewCompat.postOnAnimation(child, settleRunnable);
1194+
settleRunnable.isPosted = true;
1195+
} else {
1196+
// Otherwise, if it has been posted, just update the target state.
1197+
settleRunnable.targetState = state;
1198+
}
11851199
} else {
11861200
setStateInternal(state);
11871201
}
@@ -1356,7 +1370,9 @@ private class SettleRunnable implements Runnable {
13561370

13571371
private final View view;
13581372

1359-
@State private final int targetState;
1373+
private boolean isPosted;
1374+
1375+
@State int targetState;
13601376

13611377
SettleRunnable(View view, @State int targetState) {
13621378
this.view = view;
@@ -1368,10 +1384,9 @@ public void run() {
13681384
if (viewDragHelper != null && viewDragHelper.continueSettling(true)) {
13691385
ViewCompat.postOnAnimation(view, this);
13701386
} else {
1371-
if (state == STATE_SETTLING) {
1372-
setStateInternal(targetState);
1373-
}
1387+
setStateInternal(targetState);
13741388
}
1389+
this.isPosted = false;
13751390
}
13761391
}
13771392

tests/javatests/com/google/android/material/bottomsheet/BottomSheetBehaviorTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public static class Callback extends BottomSheetBehavior.BottomSheetCallback
7878
implements IdlingResource {
7979

8080
private boolean isIdle;
81+
private boolean idleWhileSettling;
8182

8283
private IdlingResource.ResourceCallback resourceCallback;
8384

@@ -117,9 +118,13 @@ public void registerIdleTransitionCallback(ResourceCallback callback) {
117118
resourceCallback = callback;
118119
}
119120

121+
public void setIdleWhileSettling(boolean idleWhileSettling) {
122+
this.idleWhileSettling = idleWhileSettling;
123+
}
124+
120125
private boolean isIdleState(int state) {
121126
return state != BottomSheetBehavior.STATE_DRAGGING
122-
&& state != BottomSheetBehavior.STATE_SETTLING;
127+
&& (idleWhileSettling || state != BottomSheetBehavior.STATE_SETTLING);
123128
}
124129
}
125130

0 commit comments

Comments
 (0)