@@ -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