2017-03-17 74 views
0

我需要實現與CollapsingToolbarLayout行爲類似的行爲。兩個大的差異,使這個小部件不適合我的目的:CollapsingToolbarLayout在獨立片段中的行爲

  1. 這不是工具欄的一部分。這是通過直接位於工具欄下方的片段膨脹的佈局,但不能是工具欄的子項。

  2. 當我滾動屏幕時,我想要上部框架佈局('header_fragment_container',填充了一個子片段),就像CollapsingToolbarLayout一樣摺疊。就像它那樣,我希望它被替換爲不同的佈局。

有誰知道一個合適的小部件或第三方庫,可以處理應用程序欄/工具欄以外的這種可摺疊性嗎?

下面是片段的XML作爲它存在的那一刻:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <FrameLayout 
     android:id="@+id/header_fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#fff"/> 

    <FrameLayout 
     android:id="@+id/content_fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 


</ScrollView> 

回答

0

您可以實現自定義解決方案 - 聽滾動變化,使佈局適當的修改:

scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() { 

     private static final int THRESHOLD = 150; 
     private int scrolledDistance = 0; 
     private boolean toolbarVisible = true; 

     @Override 
     public void onScrollChange(View view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 
      if (view.getScrollY() == 0) { 
       if(!toolbarVisible) { 
        toolbarVisible = true; 
        showToolbar(); 
       } 
      } else { 
       if (toolbarVisible && scrolledDistance > THRESHOLD) { 
        toolbarVisible = false; 
        scrolledDistance = 0; 
        hideToolbar(); 
       } else if (!toolbarVisible && scrolledDistance < -THRESHOLD) { 
        toolbarVisible = true; 
        scrolledDistance = 0; 
        showToolbar(); 
       } 
      } 

      if ((toolbarVisible && scrollY - oldScrollY > 0) || (!toolbarVisible && scrollY - oldScrollY < 0)) { 
       scrolledDistance += scrollY - oldScrollY; 
      } 
     } 
    }); 

private void showToolbar() { 
    // show the Toolbar and make the necessary layout modifications 
    final ObjectAnimator animator = ObjectAnimator.ofFloat(toolbar, "y", 0); 
    animator.setDuration(1000); 
    animator.start(); 
} 

private void hideToolbar() { 
    // hide the Toolbar and make the necessary layout modifications 
    final ObjectAnimator animator = ObjectAnimator.ofFloat(toolbar, "y", -getActionBarHeight()); 
    animator.setDuration(1000); 
    animator.start(); 
}