如何在android中折叠appBarLayout时去掉工具栏菜单

时间:2016-10-18 11:24:21

标签: android android-view

折叠 toolbar时,我想要消失 appBarlayout菜单,但我不知道怎么办。
我的代码在这里:

    public class MainFragment extends BaseFragment implements Toolbar.OnMenuItemClickListener ... 

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_main, container, false);

...
}
    @Override
    public int getMenu() {
        return R.menu.search;
    }

XML代码:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.sepandar.xengine.fragment.MainFragment">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimaryDark"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <com.sepandar.xengine.views.XTabLayout
                android:id="@+id/xTab"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:tabMode="scrollable"/>

            <com.api.common.widget.MyToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ToolBarStyle"/>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/xTab"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <com.nt.googleplaysearch.views.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible"
        app:layout_collapseMode="pin"/>

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

我想在折叠 appBarLayout gone public int getMenu()时,以及展开时显示此public int getMenu()

我怎么能?

2 个答案:

答案 0 :(得分:0)

使用AppBarLayout.OnOffsetChangedListener获取appbar状态{expand / collapse}, 然后在偏移值更改时隐藏appbar菜单。

答案 1 :(得分:0)

使用app:layout_scrollFlags="scroll|enterAlways" app:layout_scrollFlags="scroll|exitUntilCollapsed" MyToolbar RecyclerView NestedScrollView CoordinatorLayout ViewPager NestedScrollView作为{{1}}或{{1}}作为{{1}}的第二个元素 将您的{{1}}包裹在{{1}}