Skip to content

Commit 0356f24

Browse files
hunterstichdrchen
authored andcommitted
[Carousel] Fixed focus order when using keyboard navigation for hero carousels.
When using the keybaord to move between items, FocusFinder would occasionally skip an item because it was using the unmasked bounds of each item to compare their distance. PiperOrigin-RevId: 572025177
1 parent ed4647d commit 0356f24

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/java/com/google/android/material/carousel/MaskableFrameLayout.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.annotation.SuppressLint;
2020
import android.content.Context;
2121
import android.graphics.Canvas;
22+
import android.graphics.Rect;
2223
import android.graphics.RectF;
2324
import android.util.AttributeSet;
2425
import android.view.MotionEvent;
@@ -72,6 +73,11 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7273
}
7374
}
7475

76+
@Override
77+
public void getFocusedRect(Rect r) {
78+
r.set((int) maskRect.left, (int) maskRect.top, (int) maskRect.right, (int) maskRect.bottom);
79+
}
80+
7581
@Override
7682
protected void onAttachedToWindow() {
7783
super.onAttachedToWindow();
@@ -133,10 +139,10 @@ public void setMaskXPercentage(float percentage) {
133139

134140
private void updateMaskRectForMaskXPercentage() {
135141
if (maskXPercentage != NOT_SET) {
136-
// Translate the percentage into an actual pixel value of how much of this view should be
137-
// masked away.
138-
float maskWidth = AnimationUtils.lerp(0f, getWidth() / 2F, 0f, 1f, maskXPercentage);
139-
setMaskRectF(new RectF(maskWidth, 0F, (getWidth() - maskWidth), getHeight()));
142+
// Translate the percentage into an actual pixel value of how much of this view should be
143+
// masked away.
144+
float maskWidth = AnimationUtils.lerp(0f, getWidth() / 2F, 0f, 1f, maskXPercentage);
145+
setMaskRectF(new RectF(maskWidth, 0F, (getWidth() - maskWidth), getHeight()));
140146
}
141147
}
142148

0 commit comments

Comments
 (0)