-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 setOnTabClickListener(@Nullable OnTabClickListener onTabClickListener) { | ||
this.onTabClickListener = onTabClickListener; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Returns the custom view used for this tab. | ||
* | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -2644,7 +2669,9 @@ public boolean performClick() { | |
if (!handled) { | ||
playSoundEffect(SoundEffectConstants.CLICK); | ||
} | ||
tab.select(); | ||
if (tab.onTabClickListener != null) { | ||
tab.onTabClickListener.onClick(tab); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. tl.dr. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
return true; | ||
} else { | ||
return handled; | ||
|
There was a problem hiding this comment.
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.