Skip to content

Commit

Permalink
[TabLayout] Fix TabView click listener customization
Browse files Browse the repository at this point in the history
  • Loading branch information
vvinogra committed Mar 17, 2024
1 parent bb646b6 commit 51649be
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/java/com/google/android/material/tabs/TabLayout.java
Expand Up @@ -2173,6 +2173,8 @@ public static class Tab {
@NonNull public TabView view;
private int id = NO_ID;

private OnTabClickListener onTabClickListener = new DefaultOnTabClickListener();

// TODO(b/76413401): make package private constructor after the widget migration is finished
public Tab() {
// Private constructor
Expand Down Expand Up @@ -2219,6 +2221,17 @@ public int getId() {
return id;
}

/**
* Register a callback to be invoked when the tab is clicked.
* <p>
* Note: By default, tab is selected on click.
*/
public Tab setOnTabOnClickListener(@Nullable OnTabClickListener onTabClickListener) {
this.onTabClickListener = onTabClickListener;

return this;
}

/**
* Returns the custom view used for this tab.
*
Expand Down Expand Up @@ -2529,6 +2542,18 @@ void reset() {
contentDesc = null;
position = INVALID_POSITION;
customView = null;
onTabClickListener = new DefaultOnTabClickListener();
}

private static class DefaultOnTabClickListener implements OnTabClickListener {
@Override
public void onClick(@NonNull Tab tab) {
tab.select();
}
}

interface OnTabClickListener {
void onClick(@NonNull Tab tab);
}
}

Expand Down Expand Up @@ -2644,7 +2669,9 @@ public boolean performClick() {
if (!handled) {
playSoundEffect(SoundEffectConstants.CLICK);
}
tab.select();
if (tab.onTabClickListener != null) {
tab.onTabClickListener.onClick(tab);
}
return true;
} else {
return handled;
Expand Down

0 comments on commit 51649be

Please sign in to comment.