【问题标题】:how to gone toolbar menu when collapsed appBarLayout in android在android中折叠appBarLayout时如何消失工具栏菜单
【发布时间】:2017-02-27 15:35:56
【问题描述】:

我想消失 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>

我希望在折叠时 appBarLayoutgone public int getMenu() 以及在展开时显示此 public int getMenu()

我该怎么办?

【问题讨论】:

  • 你能把你相关的XML文件包含到工具栏和appbar代码中
  • @BhunnuBaba,请看我的更新帖!你能帮帮我吗?
  • 一旦尝试使用 appBarLayout 添加OnOffsetChangedListener
  • @BhunnuBaba,你能把代码发给我吗?请
  • @BhunnuBaba,你在这里吗?

标签: android android-view


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    MyToolbar 中使用app:layout_scrollFlags="scroll|enterAlways"app:layout_scrollFlags="scroll|exitUntilCollapsed",它与RecyclerViewNestedScrollView 一起作为CoordinatorLayout 中的第二个元素使用 将您的 ViewPager 包裹在 NestedScrollView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      相关资源
      最近更新 更多