【问题标题】:Show hidden toolbar after fragment change片段更改后显示隐藏的工具栏
【发布时间】:2016-01-17 10:00:30
【问题描述】:

我有以下问题:

main_activity:

<android.support.design.widget.CoordinatorLayout
    .......>


    <android.support.design.widget.AppBarLayout
        ......>

        <android.support.v7.widget.Toolbar
            app:layout_scrollFlags="scroll|enterAlways"
            ....../>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        ....../>

</android.support.design.widget.CoordinatorLayout>

当 FrameLayout 包含带有 recyclerview 的片段时,向上滑动 recyclerview 会向上滚动工具栏。

以下场景是问题:

  • 用户通过滚动 recyclerview 向上滚动工具栏
  • 用户从左侧滑动并打开抽屉
  • 用户然后导航到另一个页面
  • 另一个片段显示在屏幕上,但工具栏仍然隐藏

此代码执行片段更改:

fragment = .. get new fragment ..
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.commit();

添加此代码没有帮助:

getSupportActionBar().show();

我也试过了:

toolbar.translationY(0).start();

但没有成功。

如何以编程方式使向上滚动(不可见)的工具栏再次向下滚动?

【问题讨论】:

  • 没关系。 appBarLayout.setExpanded(true, true);成功了。
  • 嗯有同样的问题。 .setExpanded() 确实扩展了工具栏,但该片段中的所有内容都被向下推。你遇到过类似的事情吗?
  • 是的,我遇到了,但不幸的是我不得不暂时禁用滚动工具栏(它与我正在使用的滚动条冲突)并推迟解决这个问题。不过有一件事,我尝试在整个应用程序中仅使用一个 Activity 并仅使用 Fragments。一些片段具有滚动内容,而另一些则没有。也许使用多个活动可以解决问题?
  • 我正在使用类似的架构(主要是片段)。我最终只是在被下推的内容下方放置了一个边距以强制它备份。看起来不错,但我仍然对适当的解决方案感兴趣。感谢您的回复。
  • 如果对您有帮助,您应该提供 appBarLayout.setExpanded(true, true) 作为答案。 ;)

标签: android material-design android-5.0-lollipop android-toolbar


【解决方案1】:

我打完电话

appBarLayout.setExpanded(true, true)

在每个片段创建期间

onCreateView

每个片段都继承自抽象类的方法:

public abstract class AbstractFragmentBase extends Fragment {
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // other code
        customAppBar.setExpanded(true, true);
        // other code
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 2015-11-11
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多