我有一个关于用选项卡隐藏工具栏的问题,不幸的是我无法找到答案。在做了一些研究并实现了我的选项之后,我发现只需在工具栏 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
}
最佳答案
默认情况下,使用 CoordinatorLayout 滚动另一个 View 时隐藏/显示工具栏不适用于 ListView/GridView。您需要在它们上启用 NestedScrolling。就像下面的代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
}
这意味着它仅适用于 Lollipop 及以上设备。
归功于Gabriele
关于android - Android 中使用 listview 隐藏工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37450660/