使用listview Android隐藏工具栏

时间:2016-05-26 02:40:20

标签: android xml tabs android-toolbar

我有一个关于使用标签隐藏工具栏的问题,遗憾的是我无法找到答案。在做了一些研究并实现我的选项后,我发现只需添加app:layout_scrollFlags =“scroll | enterAlways”/>工具栏里面的xml应该做它所做的技巧,但遗憾的是它只是在我自己向上滚动时隐藏工具栏,而不是在向上和向下滚动列表(位于选项卡内)时。此外,我注意到当我向上滚动工具栏时,它隐藏了时间所在的最顶部。作为参考,我使用本教程创建了我的标签工具栏:http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

如果您有任何想法或希望我提供更多信息,请与我们联系。提前谢谢!

XML:

while (true) {

 System.out.printf("Your Point: %d\n", sum); 
 dice1 = (int)(Math.random()*6) + 1;
 dice2 = (int)(Math.random()*6) + 1;
 sum = dice1 + dice2;
 System.out.printf("You rolled: %d %s %d %s %d\n",dice1,"+",dice2,"=",sum);

 if (point == sum){ 
     System.out.println("You Win!");      
     break;
 }
 if (point == 7){
    System.out.println("You Lose!");
    break;
 }

 System.out.println("trying again");

}

JAVA:

<?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>

1 个答案:

答案 0 :(得分:2)

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

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

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

归功于Gabriele