CollapsingToolbarLayout + Toolbar结合使用minHeight不生效源码分析

使用CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout可以实现折叠效果

CollapsingToolbarLayout + Toolbar组合使用可以实现header固定在页面顶部,但是有时候希望给CollapsingToolbarLayout设置一个最小高度,使页面最大只能滚动到指定的一个点,但是此时给CollapsingToolbarLayout设置minHeight不生效。分析CollapsingToolbarLayout源码找到了原因,记录一下。

先放自己看源码得到的结论:

  • 存在Toolbar作为CollapsingToolbarLayout的直接子元素, 那么不管自己有没有单独设置minHeight,Toolbar的高度将作为CollapsingToolbarLayout的minHeight.
  • 存在Toolbar, 但是不是直接子元素, maybe孙子节点. 如果CollapsingToolbarLayout设置了toolbarId属性, 则包含了引用的Toolbar元素,且是CollapsingToolbarLayout的直接子元素的视图高度将会作为CollapsingToolbarLayout的minHeight. 如果没有设置toolbarId, 视图将会一起滑动

CollapsingToolbarLayout代码分析:
ensureToolbar方法:

  private void ensureToolbar() {
 	...
 	// 1
    if (toolbarId != -1) {
      // If we have an ID set, try and find it and it's direct parent to us
      this.toolbar = findViewById(toolbarId);
      if (this.toolbar != null) {
        toolbarDirectChild = findDirectChild(this.toolbar);
      }
    }
    
    // 2
    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;
      for (int i = 0, count = getChildCount(); i < count; i++) {
        final View child = getChildAt(i);
        if (child instanceof Toolbar) {
          toolbar = (Toolbar) child;
          break;
        }
      }
      this.toolbar = toolbar;
    }
    ...
  }

代码块1: 源码也添加了注释

If we have an ID set, try and find it and it’s direct parent to us

如果CollapsingToolbarLayout有设置toolbarId属性的话,则会查找toolbar以及包含此Toolbar且是CollapsingToolbarLayout直接子元素的视图。
代码块2:如果没有设置toolbarId, 或者指定toolbarId不存在,则会去遍历CollapsingToolbarLayout的子元素查找Toolbar,注意是直接子元素. 这种情况下toolbarDirectChild总是null的

再看onLayout方法:

@Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    ...
    // Set our minimum height to enable proper AppBarLayout collapsing
    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());
      }
      if (toolbarDirectChild == null || toolbarDirectChild == this) {
        setMinimumHeight(getHeightWithMargins(toolbar));
      } else {
        setMinimumHeight(getHeightWithMargins(toolbarDirectChild));
      }
    }
   ...
  }

如果Toolbar不为空的话,给CollapsingToolbarLayout设置minHeight。所以只要在ensureToolbar方法里找到了Toolbar子元素,就会给CollapsingToolbarLayout设置一个最小高度,滑出屏幕的时候始终都会有最小高度显示在屏幕上.

上面所说的minHeight是不能被覆盖的,因为即使在代码中手动设置CollapsingToolbarLayout的minHeight时会调用requestLayout, 还是会触发onLayout方法,Toolbar的高度最终会覆盖手动设置minHeight

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。针对你的问题,可以通过如下代码实现AppBarLayout的滚动监听: ```java AppBarLayout appBarLayout = findViewById(R.id.appBarLayout); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { // 在这里处理滚动事件 } }); ``` 其中,`addOnOffsetChangedListener` 方法可以添加滚动监听器,该监听器中的 `onOffsetChanged` 方法中可以处理 AppBarLayout 的滚动事件。 关于CoordinatorLayout、AppBarLayoutCollapsingToolbarLayout 和 RecyclerView 的结合使用,可以参考以下代码: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collapsingToolbarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/image" app:layout_collapseMode="parallax" /> <com.google.android.material.toolbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 其中,`CoordinatorLayout` 是一个可以协调多个子视图之间交互的布局容器。`AppBarLayout` 是一个垂直的 LinearLayout,可以包含一个或多个子视图,用于实现应用程序栏。`CollapsingToolbarLayout` 是一个具有可折叠效果的 Toolbar 布局。`RecyclerView` 是一个可以滚动的列表视图。 以上代码实现了一个具有可折叠效果的 Toolbar 和一个可以滚动的列表视图。在 `CollapsingToolbarLayout` 中的 `ImageView` 和 `Toolbar` 都有一个 `layout_collapseMode` 属性,用于指定它们的行为。`ImageView` 的 `layout_collapseMode` 属性设置为 `parallax`,表示在滚动时具有视差效果;`Toolbar` 的 `layout_collapseMode` 属性设置为 `pin`,表示在折叠时固定在顶部。 `RecyclerView` 的 `layout_behavior` 属性设置为 `appbar_scrolling_view_behavior`,表示它是一个可以与 `AppBarLayout` 协同工作的滚动视图。这样,`AppBarLayout` 就可以根据 `RecyclerView` 的滚动情况来调整自身的显示状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值