2016-05-26 60 views
1

我有一個關於隱藏工具欄與選項卡的問題,不幸的是我一直無法找到我的答案。在做了一些研究並實現了我的選項之後,我發現只需在工具欄xml中添加app:layout_scrollFlags =「scroll | enterAlways」/>就可以完成它的功能,但不幸的是,當我自己向上滾動時,它只隱藏工具欄而不是在上下滾動列表(位於標籤內)時。另外,我注意到當我向上滾動工具欄時,它隱藏了時間所在的頂部。作爲參考,我使用本教程創建了我的選項卡工具欄:http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/隱藏工具欄與列表視圖Android

如果您有任何想法或希望我提供更多信息,請讓我知道。先謝謝你!

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:滾動與CoordinatorLayout另一個View默認情況下不使用的ListView/GridView的工作時,

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 
} 
+1

什麼是content_main和什麼是「列表」? – tachyonflux

+1

嘗試找到lib在https://github.com/wasabeef/awesome-android-ui –

+0

你好@karaokyo content_main與我添加的模板,當我用android studio開始一個項目,目前這是空的。列表視圖位於每個選項卡內。希望這有助於使更多的意義謝謝你! – paul590

回答

1

隱藏/顯示工具欄。你需要在它們上啓用NestedScrolling。像下面的代碼

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

這意味着它只適用於棒棒糖和以上設備。

貸記Gabriele

+0

這解決了我的問題!對不起,謝謝你的幫助! – paul590

+1

謝謝你回來接受答案。 –

+0

沒問題!它幫助我解決了我的問題! – paul590