使用列表视图隐藏工具栏 Android

Hiding Toolbar with listview Android

我有一个关于用选项卡隐藏工具栏的问题,很遗憾我找不到答案。在做了一些研究并实施了我的选项之后,我发现只需在工具栏 xml 中添加 app:layout_scrollFlags="scroll|enterAlways"/> 就可以达到它的效果,但不幸的是它只隐藏了工具栏当我自己向上滚动而不是上下滚动列表(位于选项卡内)时。另外,我注意到当我向上滚动工具栏时,它隐藏了时间所在的最顶部。作为参考,我使用本教程创建了我的选项卡工具栏:http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

如果您有任何想法或希望我提供更多信息,请告诉我。提前致谢!

XML:

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

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

    <include layout="@layout/content_main" />

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

JAVA:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        //Some other code
}

Hide/Show 默认情况下,使用 CoordinatorLayout 滚动另一个视图时工具栏不适用于 ListView/GridView。您需要在它们上启用 NestedScrolling。喜欢下面的代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     listView.setNestedScrollingEnabled(true);
}

这意味着它仅适用于 Lollipop 及更高版本的设备。

归功于