25
25
import static com .google .android .material .animation .AnimationUtils .FAST_OUT_SLOW_IN_INTERPOLATOR ;
26
26
import static com .google .android .material .theme .overlay .MaterialThemeOverlay .wrap ;
27
27
28
- import android .animation .Animator ;
29
- import android .animation .AnimatorListenerAdapter ;
30
28
import android .animation .ValueAnimator ;
31
29
import android .annotation .SuppressLint ;
32
30
import android .content .Context ;
@@ -3007,10 +3005,6 @@ private float approximateLineWidth(@NonNull Layout layout, int line, float textS
3007
3005
3008
3006
class SlidingTabIndicator extends LinearLayout {
3009
3007
ValueAnimator indicatorAnimator ;
3010
- int selectedPosition = -1 ;
3011
- // selectionOffset is only used when a tab is being slid due to a viewpager swipe.
3012
- // selectionOffset is always the offset to the right of selectedPosition.
3013
- float selectionOffset ;
3014
3008
3015
3009
private int layoutDirection = -1 ;
3016
3010
@@ -3038,26 +3032,21 @@ boolean childrenNeedLayout() {
3038
3032
/**
3039
3033
* Set the indicator position based on an offset between two adjacent tabs.
3040
3034
*
3041
- * @param position The position from which the offset should be calculated .
3042
- * @param positionOffset The offset to the right of position where the indicator should be
3043
- * drawn. This must be a value between 0.0 and 1.0 .
3035
+ * @param position Position index of the first tab (with less index) currently being displayed .
3036
+ * Tab position+1 will be visible if positionOffset is nonzero.
3037
+ * @param positionOffset Value from [0, 1) indicating the offset from the tab at position .
3044
3038
*/
3045
3039
void setIndicatorPositionFromTabPosition (int position , float positionOffset ) {
3046
3040
if (indicatorAnimator != null && indicatorAnimator .isRunning ()) {
3047
3041
indicatorAnimator .cancel ();
3048
3042
}
3049
3043
3050
- selectedPosition = position ;
3051
- selectionOffset = positionOffset ;
3044
+ // The title view refers to the one indicated when offset is 0.
3045
+ final View firstTitle = getChildAt (position );
3046
+ // The title view refers to the one indicated when offset is 1.
3047
+ final View nextTitle = getChildAt (position + 1 );
3052
3048
3053
- final View selectedTitle = getChildAt (selectedPosition );
3054
- final View nextTitle = getChildAt (selectedPosition + 1 );
3055
-
3056
- tweenIndicatorPosition (selectedTitle , nextTitle , selectionOffset );
3057
- }
3058
-
3059
- float getIndicatorPosition () {
3060
- return selectedPosition + selectionOffset ;
3049
+ tweenIndicatorPosition (firstTitle , nextTitle , positionOffset );
3061
3050
}
3062
3051
3063
3052
@ Override
@@ -3144,7 +3133,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
3144
3133
// position of the indicator, since the tab widths are different. We need to modify the
3145
3134
// animation's updateListener to pick up the new target positions.
3146
3135
updateOrRecreateIndicatorAnimation (
3147
- /* recreateAnimation= */ false , selectedPosition , /* duration= */ -1 );
3136
+ /* recreateAnimation= */ false , getSelectedTabPosition () , /* duration= */ -1 );
3148
3137
} else {
3149
3138
// If we've been laid out, update the indicator position
3150
3139
jumpIndicatorToSelectedPosition ();
@@ -3153,7 +3142,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
3153
3142
3154
3143
/** Immediately update the indicator position to the currently selected position. */
3155
3144
private void jumpIndicatorToSelectedPosition () {
3156
- final View currentView = getChildAt (selectedPosition );
3145
+ final View currentView = getChildAt (getSelectedTabPosition () );
3157
3146
tabIndicatorInterpolator .setIndicatorBoundsForTab (
3158
3147
TabLayout .this , currentView , tabSelectedIndicator );
3159
3148
}
@@ -3214,7 +3203,7 @@ void animateIndicatorToPosition(final int position, int duration) {
3214
3203
*/
3215
3204
private void updateOrRecreateIndicatorAnimation (
3216
3205
boolean recreateAnimation , final int position , int duration ) {
3217
- final View currentView = getChildAt (selectedPosition );
3206
+ final View currentView = getChildAt (getSelectedTabPosition () );
3218
3207
final View targetView = getChildAt (position );
3219
3208
if (targetView == null ) {
3220
3209
// If we don't have a view, just update the position now and return
@@ -3239,18 +3228,6 @@ public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
3239
3228
animator .setDuration (duration );
3240
3229
animator .setFloatValues (0F , 1F );
3241
3230
animator .addUpdateListener (updateListener );
3242
- animator .addListener (
3243
- new AnimatorListenerAdapter () {
3244
- @ Override
3245
- public void onAnimationStart (Animator animator ) {
3246
- selectedPosition = position ;
3247
- }
3248
-
3249
- @ Override
3250
- public void onAnimationEnd (Animator animator ) {
3251
- selectedPosition = position ;
3252
- }
3253
- });
3254
3231
animator .start ();
3255
3232
} else {
3256
3233
// Reuse the existing animator. Updating the listener only modifies the target positions.
0 commit comments