[Slider] Fix slider tooltip in popupwindows

Slider needs to find an ancestor in which it can add its tooltip overlay. When added in a PopupWindow, its possible to search up the view hierarchy and find a parent which is neither a View nor a ViewGroup. This change adds a check for this case

A note is that this change updates edge cases to return the current parent if it is any valid ancestor instead of just returning null. This allows Slider's tooltip to be added to Popup windows, but doesn't guarantee that the tooltip will be fully visible since a PopupWindow's height might be smaller than the slider + tooltip. If adding a Slider to a PopupWindow, you might need to manually add height to your popup's content view to account for the tooltip or turn of the slider's label.

PiperOrigin-RevId: 310913776
(cherry picked from commit b631ef697984c695e1a16b1017b30f035a7599ce)
This commit is contained in:
hunterstich 2020-05-11 11:10:16 -04:00 committed by Daniel Nizri
parent 649d97276c
commit aab7100fa9

View File

@ -284,19 +284,24 @@ public class ViewUtils {
/** Returns the content view that is the parent of the provided view. */
@Nullable
public static ViewGroup getContentView(@Nullable View view) {
View parent = view;
while (parent != null) {
if (parent.getId() == android.R.id.content && parent instanceof ViewGroup) {
return (ViewGroup) parent;
}
if (parent.getParent() instanceof ViewGroup) {
parent = (ViewGroup) parent.getParent();
} else if (parent.getParent() == null) {
// If android.R.id.content has not been found and parent has no more parents to search,
// exit.
return null;
}
if (view == null) {
return null;
}
View rootView = view.getRootView();
ViewGroup contentView = rootView.findViewById(android.R.id.content);
if (contentView != null) {
return contentView;
}
// Account for edge cases: Parent's parent can be null without ever having found
// android.R.id.content (e.g. if view is in an overlay during a transition).
// Additionally, sometimes parent's parent is neither a ViewGroup nor a View (e.g. if view
// is in a PopupWindow).
if (rootView != view && rootView instanceof ViewGroup) {
return (ViewGroup) rootView;
}
return null;
}