Skip to content

Commit 4e8510b

Browse files
committed
Track offset of center child view in Gallery for later layouts
When apps request layouts while scrolling is in progress, Gallery children can get offset in strange ways. Compensate for this by tracking the last known offset and applying it during layout. Bug 7245853 Change-Id: I9d746ae6bb06918e2d920c58052f72e749a7ffbf
1 parent 59adf04 commit 4e8510b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

core/java/android/widget/Gallery.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ public void run() {
182182
*/
183183
private boolean mIsRtl = true;
184184

185+
/**
186+
* Offset between the center of the selected child view and the center of the Gallery.
187+
* Used to reset position correctly during layout.
188+
*/
189+
private int mSelectedCenterOffset;
190+
185191
public Gallery(Context context) {
186192
this(context, null);
187193
}
@@ -395,6 +401,14 @@ void trackMotionScroll(int deltaX) {
395401

396402
setSelectionToCenterChild();
397403

404+
final View selChild = mSelectedChild;
405+
if (selChild != null) {
406+
final int childLeft = selChild.getLeft();
407+
final int childCenter = selChild.getWidth() / 2;
408+
final int galleryCenter = getWidth() / 2;
409+
mSelectedCenterOffset = childLeft + childCenter - galleryCenter;
410+
}
411+
398412
onScrollChanged(0, 0, 0, 0); // dummy values, View's implementation does not use these.
399413

400414
invalidate();
@@ -537,6 +551,7 @@ private void onFinishedMovement() {
537551
// We haven't been callbacking during the fling, so do it now
538552
super.selectionChanged();
539553
}
554+
mSelectedCenterOffset = 0;
540555
invalidate();
541556
}
542557

@@ -650,7 +665,8 @@ void layout(int delta, boolean animate) {
650665
View sel = makeAndAddView(mSelectedPosition, 0, 0, true);
651666

652667
// Put the selected child in the center
653-
int selectedOffset = childrenLeft + (childrenWidth / 2) - (sel.getWidth() / 2);
668+
int selectedOffset = childrenLeft + (childrenWidth / 2) - (sel.getWidth() / 2) +
669+
mSelectedCenterOffset;
654670
sel.offsetLeftAndRight(selectedOffset);
655671

656672
fillToGalleryRight();

0 commit comments

Comments
 (0)