Skip to content

Commit

Permalink
[TabLayout] tabs with padding in fixed mode shouldn't scroll
Browse files Browse the repository at this point in the history
Resolves #2410

PiperOrigin-RevId: 432032683
  • Loading branch information
veganafro authored and afohrman committed Mar 3, 2022
1 parent 9cdf4c9 commit 9c2df28
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/java/com/google/android/material/tabs/TabLayout.java
Expand Up @@ -28,6 +28,7 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
Expand All @@ -54,6 +55,7 @@
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -778,6 +780,27 @@ private void addTabFromItemView(@NonNull TabItem item) {
addTab(tab);
}

private boolean isScrollingEnabled() {
return getTabMode() == MODE_SCROLLABLE || getTabMode() == MODE_AUTO;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// When a touch event is intercepted and the tab mode is fixed, do not continue to process the
// touch event. This will prevent unexpected scrolling from occurring in corner cases (i.e. a
// layout in fixed mode that has padding should not scroll for the width of the padding).
return isScrollingEnabled() && super.onInterceptTouchEvent(event);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_SCROLL && !isScrollingEnabled()) {
return false;
}
return super.onTouchEvent(event);
}

/**
* @deprecated Use {@link #addOnTabSelectedListener(OnTabSelectedListener)} and {@link
* #removeOnTabSelectedListener(OnTabSelectedListener)}.
Expand Down

0 comments on commit 9c2df28

Please sign in to comment.