3

我正在使用AppCompat-v21的新工具欄。我想要做的是在用戶開始向下滾動時隱藏操作欄,並在用戶向上滾動時再次顯示操作欄。它就像谷歌播放應用程序。我還在工具欄下方有一個標籤佈局,例如Google Play應用。那需要留在那裏。使用android棒棒糖工具欄快速返回操作欄

我試過使用谷歌IO應用程序的代碼。但是當工具欄消失時,它會使工具欄消失並且主內容不會向上移動。工具欄之前還有一個空白空間。也許是因爲該實現僅適用於列表和網格視圖。

任何想法如何做到這一點?

+0

Hi Alam,您是否找到針對您的問題的解決方案? – Adelino 2014-11-19 14:13:37

+0

@Adelino不,我沒有。使用谷歌IO應用程序的來源,我設法做到了一定程度。仍然沒有按我想要的方式工作。 – 2014-11-20 07:54:05

+1

嗨阿拉姆,搜索好的例子後,我發現這個https://github.com/ksoichiro/Android-ObservableScrollView回購,我只是拿着我需要的,它爲我工作。 – Adelino 2014-12-04 12:00:19

回答

2

你需要做的基本上是創建自己的動作條主題這臺windowActionBarOverlay爲true什麼:

<!-- the theme applied to the application or activity --> 
<style name="CustomActionBarTheme" 
     parent="@android:style/Theme.AppCompat"> 
    <item name="android:windowActionBarOverlay">true</item> 

    <!-- Support library compatibility --> 
    <item name="windowActionBarOverlay">true</item> 
</style> 

這不正是因爲它的聲音 - 它坐落在當前視圖的頂部。但是,這也意味着您需要在主視圖頂部添加一個額外的邊距,以考慮ActionBar疊加層。

<!-- Support library compatibility --> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="?attr/actionBarSize"> 
    ... 
</RelativeLayout> 

一旦你獲得了你的自定義ActionBar主題,你就不應該再看到額外的空間了。這些代碼片段直接從Android Developer Training Guide中獲取。按照鏈接獲取有關ActionBarOverlay如何工作的更多詳細信息。

0

更簡單的方法是使用app:layout_scrollFlags

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

所以工具欄會自動檢測滾動並開始隱藏或顯示。

P.S.如果您使用的是ListView,則需要將其更改爲RecycleView

相關問題