【问题标题】:Hiding Toolbar with listview Android使用 listview Android 隐藏工具栏
【发布时间】:2016-05-26 02:40:19
【问题描述】:

我有一个关于隐藏带有标签的工具栏的问题,很遗憾我找不到答案。在做了一些研究并实现了我的选项之后,我发现只需在工具栏 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
}

【问题讨论】:

  • 什么是content_main,什么是“列表”?
  • Hello @karaokyo content_main 附带了我在 android studio 开始项目时添加的模板,目前这是空的。列表视图位于每个选项卡内。希望这有助于更有意义,谢谢!
  • @NguyễnTrungHiếu 谢谢我目前正在尝试在不使用外部库的情况下创建,我以此为指导mzgreen.github.io/2015/06/23/…
  • 有什么想法吗?谢谢

标签: android xml tabs android-toolbar


【解决方案1】:

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

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

这意味着它仅适用于 Lollipop 及以上设备。

感谢Gabriele

【讨论】:

  • 这解决了我的问题!抱歉耽搁了谢谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多