2014-12-13 54 views
5

我想,當用戶滾動列表,以便在我的工具欄隱藏或顯示。要做到這一點,我使用的翻譯,但出現空白,而不是我的actionBar。如果我使用一個setVisibility(View.GONE),空白空間將在動畫中出現,隱藏當它這樣做是醜..Android的工具欄顯示/隱藏空白

Here is a short video of my issue

這裏是我做我的動畫(從谷歌I/O的應用程序):

public void showToolbar(boolean show){ 
    if (show) { 
     toolbar.animate() 
       .translationY(0) 
       .alpha(1) 
       .setDuration(HEADER_HIDE_ANIM_DURATION) 
       .setInterpolator(new DecelerateInterpolator()); 
    } else { 
     toolbar.animate() 
       .translationY(-toolbar.getBottom()) 
       .alpha(0) 
       .setDuration(HEADER_HIDE_ANIM_DURATION) 
       .setInterpolator(new DecelerateInterpolator()); 
    } 
} 

這裏是我的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/mainContent"> 

<include layout="@layout/toolbar" 
    android:id="@+id/my_toolbar" /> 

<fragment 
    android:name="com.ar.oe.fragments.SectionsFragment" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="?attr/actionBarSize"/> 

</RelativeLayout> 

而且我的工具欄

<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/my_toolbar" 
android:layout_height="?attr/actionBarSize" 
android:layout_width="match_parent" 
android:minHeight="?attr/actionBarSize" 
android:background="?attr/colorPrimary" 
/> 
+1

有你設法解決這一問題? – chip 2015-02-18 06:54:35

回答

1

當用戶試圖滾動時,您最好顯示爲工具欄和下面的片段(一起)滑動翻譯動畫,以便只有工具欄離開視圖並且片段到達頂部。 (在200ms內)。要做到這一點,翻譯的整個外相對佈局被說約20%(你可以改變,使得只有工具欄超出視圖):

在你的動畫文件夾添加slide_up.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate 
    android:fillAfter="false" 
    android:fromYDelta="0%" 
    android:toYDelta="-20%" 
    android:duration="200"/> 

</set> 

然後,當觸發滾動事件,請執行下列操作:

... 
RelativeLayout rel = (RelativeLayout)findViewById(R.id.mainContent); 
Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up); 
rel.startAnimation(slideUp); 
... 

希望這有助於...

+0

這將仍然有相同數量的黑色空間,但在底部......!但有沒有辦法告訴容器(片段)堅持工具欄的底部,並不斷更新佈局,以保持它堅持工具欄底部? – 2016-04-28 17:00:11

+0

任何解決方案如何刪除底部的黑色空間? – Ram 2016-08-02 10:41:21

3

您需要動畫的容器,而不是你的工具欄。

試試這個:

RelativeLayout mainContent = (RelativeLayout) findById(R.id.mainContent); 

public void showToolbar(boolean show){ 
if (show) { 
    mainContent.animate() 
      .translationY(0) 
      .alpha(1) 
      .setDuration(HEADER_HIDE_ANIM_DURATION) 
      .setInterpolator(new DecelerateInterpolator()); 
} else { 
    mainContent.animate() 
      .translationY(-toolbar.getBottom()) 
      .alpha(0) 
      .setDuration(HEADER_HIDE_ANIM_DURATION) 
      .setInterpolator(new DecelerateInterpolator()); 
} 
} 

這對我的工作;)

如果你想與你的工具欄移到您的片段,你需要你的動畫片段,並不僅是你的吧。

+0

呼叫需要API級別12 ... – GeneralKimi 2015-04-13 14:49:59

+1

但它將使從頂部的空白空間移至底部,並添加效果反彈向上和向下反彈,如果汽車無持有我們的手指時的動畫效果發生 – HendraWD 2016-01-13 06:19:38

+0

任何解決如何去除在底部的黑色空間? – Ram 2016-08-02 10:41:40