【问题标题】:Collapsing Toolbar Layout always expanded when returned from fragment从片段返回时,折叠工具栏布局总是展开
【发布时间】:2016-12-14 13:07:18
【问题描述】:

我有 2 个片段(片段 A 和片段 B),它们都具有可折叠的工具栏布局和 coordinatorlayout 中的相应回收器视图。

如果我在我的 recyclerview 中向上滚动(因此 CollapsingToolbarLayout 已折叠),然后从片段 A 打开片段 B(将 A 推到后台堆栈上)。

当我通过回击返回片段 A 时。 CollapsingToolbarLayout/AppBarLayout 总是展开的,即使回收站视图在同一个位置。

有人经历过吗?

【问题讨论】:

    标签: android-fragments android-recyclerview android-appbarlayout android-collapsingtoolbarlayout


    【解决方案1】:

    有一个与此相关的问题。 根据Chris Banes.

    在片段的 onViewCreated() 中添加这些行来解决问题。

    ViewCompat.requestApplyInsets(mCoordinatorLayout);
    

    【讨论】:

    • 解释一下你做了什么?
    【解决方案2】:

    这是一个老问题,但没有一个答案是正确的。我发现了他们解决问题的另一个问题:

    How to restore Collapsing Toolbar Layout State after screen orientation change

    您必须为视图设置一个 id 才能自动恢复它们的状态。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,所以我写了下面的代码:-

      private boolean isExpand = true;
      private void setTitleNotExpand(boolean isExpand) {
          if(getFragmentViewHolder() != null) {
              this.isExpand = isExpand;
              // AppBarLayout variable
              getFragmentViewHolder().appbar.setExpanded(isExpand);
          }
      }
      

      当您添加回堆栈时,请编写以下代码:-

      // write below code where you want to stick your toolbar
      setTitleNotExpand(false);
      
      
      // write below code where you want not to stick your toolbar
      setTitleNotExpand(true);
      

      在您的onFragmentViewHolderCreated 上写下以下代码:-

      getFragmentViewHolder().appbar.setExpanded(isExpand);
      

      【讨论】:

        【解决方案4】:

        要考虑的另一件事是,如果视图没有 id,则它们无法恢复其状态

        【讨论】:

          【解决方案5】:

          今天,要解决此问题,您只需为协调器布局设置一个 id 即可解决此问题。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多