Skip to content

Commit

Permalink
[CollapsingToolbarLayout] Updated CollapsingToolbarLayout to support …
Browse files Browse the repository at this point in the history
…framework android.widget.Toolbar (<Toolbar)

PiperOrigin-RevId: 345236307
(cherry picked from commit 036cff7)
  • Loading branch information
dsn5ft committed Dec 15, 2020
1 parent 7915087 commit d568e55
Showing 1 changed file with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.util.ObjectsCompat;
import androidx.core.view.GravityCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
Expand Down Expand Up @@ -117,7 +118,7 @@ public class CollapsingToolbarLayout extends FrameLayout {

private boolean refreshToolbar = true;
private int toolbarId;
@Nullable private Toolbar toolbar;
@Nullable private ViewGroup toolbar;
@Nullable private View toolbarDirectChild;
private View dummyView;

Expand Down Expand Up @@ -373,11 +374,11 @@ private void ensureToolbar() {
if (this.toolbar == null) {
// If we don't have an ID, or couldn't find a Toolbar with the correct ID, try and find
// one from our direct children
Toolbar toolbar = null;
ViewGroup toolbar = null;
for (int i = 0, count = getChildCount(); i < count; i++) {
final View child = getChildAt(i);
if (child instanceof Toolbar) {
toolbar = (Toolbar) child;
if (isToolbar(child)) {
toolbar = (ViewGroup) child;
break;
}
}
Expand All @@ -388,6 +389,11 @@ private void ensureToolbar() {
refreshToolbar = false;
}

private static boolean isToolbar(View view) {
return view instanceof androidx.appcompat.widget.Toolbar
|| (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && view instanceof android.widget.Toolbar);
}

private boolean isToolbarChild(View child) {
return (toolbarDirectChild == null || toolbarDirectChild == this)
? child == toolbar
Expand Down Expand Up @@ -485,21 +491,15 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;

// Update the collapsed bounds
final int maxOffset =
getMaxOffsetForPinChild(toolbarDirectChild != null ? toolbarDirectChild : toolbar);
DescendantOffsetUtils.getDescendantRect(this, dummyView, tmpRect);
collapsingTextHelper.setCollapsedBounds(
tmpRect.left + (isRtl ? toolbar.getTitleMarginEnd() : toolbar.getTitleMarginStart()),
tmpRect.top + maxOffset + toolbar.getTitleMarginTop(),
tmpRect.right - (isRtl ? toolbar.getTitleMarginStart() : toolbar.getTitleMarginEnd()),
tmpRect.bottom + maxOffset - toolbar.getTitleMarginBottom());
updateCollapsedBounds(isRtl);

// Update the expanded bounds
collapsingTextHelper.setExpandedBounds(
isRtl ? expandedMarginEnd : expandedMarginStart,
tmpRect.top + expandedMarginTop,
right - left - (isRtl ? expandedMarginStart : expandedMarginEnd),
bottom - top - expandedMarginBottom);

// Now recalculate using the new bounds
collapsingTextHelper.recalculate();
}
Expand All @@ -508,7 +508,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
if (toolbar != null) {
if (collapsingTitleEnabled && TextUtils.isEmpty(collapsingTextHelper.getText())) {
// If we do not currently have a title, try and grab it from the Toolbar
setTitle(toolbar.getTitle());
setTitle(getToolbarTitle(toolbar));
}
}

Expand All @@ -520,6 +520,50 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
}
}

private void updateCollapsedBounds(boolean isRtl) {
final int maxOffset =
getMaxOffsetForPinChild(toolbarDirectChild != null ? toolbarDirectChild : toolbar);
DescendantOffsetUtils.getDescendantRect(this, dummyView, tmpRect);
final int titleMarginStart;
final int titleMarginEnd;
final int titleMarginTop;
final int titleMarginBottom;
if (toolbar instanceof androidx.appcompat.widget.Toolbar) {
androidx.appcompat.widget.Toolbar compatToolbar = (androidx.appcompat.widget.Toolbar) toolbar;
titleMarginStart = compatToolbar.getTitleMarginStart();
titleMarginEnd = compatToolbar.getTitleMarginEnd();
titleMarginTop = compatToolbar.getTitleMarginTop();
titleMarginBottom = compatToolbar.getTitleMarginBottom();
} else if (VERSION.SDK_INT >= VERSION_CODES.N && toolbar instanceof android.widget.Toolbar) {
android.widget.Toolbar frameworkToolbar = (android.widget.Toolbar) toolbar;
titleMarginStart = frameworkToolbar.getTitleMarginStart();
titleMarginEnd = frameworkToolbar.getTitleMarginEnd();
titleMarginTop = frameworkToolbar.getTitleMarginTop();
titleMarginBottom = frameworkToolbar.getTitleMarginBottom();
} else {
titleMarginStart = 0;
titleMarginEnd = 0;
titleMarginTop = 0;
titleMarginBottom = 0;
}
collapsingTextHelper.setCollapsedBounds(
tmpRect.left + (isRtl ? titleMarginEnd : titleMarginStart),
tmpRect.top + maxOffset + titleMarginTop,
tmpRect.right - (isRtl ? titleMarginStart : titleMarginEnd),
tmpRect.bottom + maxOffset - titleMarginBottom);
}

private static CharSequence getToolbarTitle(View view) {
if (view instanceof androidx.appcompat.widget.Toolbar) {
return ((androidx.appcompat.widget.Toolbar) view).getTitle();
} else if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP
&& view instanceof android.widget.Toolbar) {
return ((android.widget.Toolbar) view).getTitle();
} else {
return null;
}
}

private static int getHeightWithMargins(@NonNull final View view) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof MarginLayoutParams) {
Expand Down

0 comments on commit d568e55

Please sign in to comment.