Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TabLayout] Fix TabView click listener customization #4107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vvinogra
Copy link

@vvinogra vvinogra commented Mar 17, 2024

Closes #4105

Usage example:

TabLayout tabLayout = ...;

Tab firstTab = tabLayout.newTab()
    .setText("Tab 1")
    .setOnTabClickListener((tab) -> {
        if (!isSomeCheckValid) {
            // Show some error dialog.
            return;
        }

        tab.select();
     });

tabLayout.addTab(firstTab);

Copy link

google-cla bot commented Mar 17, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@vvinogra vvinogra force-pushed the master branch 3 times, most recently from a9385fb to 51649be Compare March 17, 2024 10:49
tab.select();
if (tab.onTabClickListener != null) {
tab.onTabClickListener.onClick(tab);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to avoid overriding the default behavior unless there's a valid use case to do that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drchen Hey, thanks for the comment.

The main idea is to have an option to override a default behavior.
A usecase is described in the #4105 issue.

tl.dr.
We need to override a default behaviour to add a validation logic when user clicks on the tab before selecting it.
With the existing implementation, I tried selecting the previous tab in case the data is not valid but it doesn't look good because the tab is selected with the animation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool. So I think there are several improvements I'll suggest here:

  1. Let's make onTabClickListener be non-null.
  2. Let OnTabClickListener .onClick() return a boolean to indicate if the click event is handled, so we can simplify the logic like:
final boolean handled = super.performClick() || tab.onTabClickListener.onClick(tab);
... rest the same
  1. Refine the javadoc to make it more clear that how to override the default behavior.

@vvinogra vvinogra requested a review from drchen March 21, 2024 15:50
tab.select();
if (tab.onTabClickListener != null) {
tab.onTabClickListener.onClick(tab);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool. So I think there are several improvements I'll suggest here:

  1. Let's make onTabClickListener be non-null.
  2. Let OnTabClickListener .onClick() return a boolean to indicate if the click event is handled, so we can simplify the logic like:
final boolean handled = super.performClick() || tab.onTabClickListener.onClick(tab);
... rest the same
  1. Refine the javadoc to make it more clear that how to override the default behavior.

@@ -2644,7 +2669,9 @@ public boolean performClick() {
if (!handled) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be part of the default onClick behavior as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TabLayout] Can't customize TabView click listener
2 participants