2

爲我的最後一個項目添色我發現了Android Material Design Library。它非常強大,我很樂意與它合作。我向FloatingActionButton添加了一個自定義行爲,所以它在向下滾動時消失。現在我提到,如果顯示一個SnackBar,FAB的位置不會自動處理。將一個以上的行爲添加到FloatingActionButton

經過一些調試後,我發現將anchor設置爲recyclerView,並添加customBehaviour以根據SnackBar滾動CoordinatorLayout的默認行爲已消失。

所以我問自己,我可以給我的FAB添加更多的一個行爲嗎?或者我可以以某種方式告訴它,不應該被覆蓋,但延長?

或者我可以寫多個這些嗎?

@Override 
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { 
    return dependency instanceof RecyclerView; 
} 

回答

0

好吧,我找到了一種方法來實現希望的行爲,但它有一個解決方法比答案更多。

如果我在Java代碼中以編程方式添加的Scrollbehavior這樣的:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
      if (newState == RecyclerView.SCROLL_STATE_IDLE) { 
       sendMailFAB.show(); 
      } 
      super.onScrollStateChanged(recyclerView, newState); 
     } 

     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      super.onScrolled(recyclerView, dx, dy); 
      if (dy > 0 && sendMailFAB.isShown()) 
       sendMailFAB.hide(); 
     } 
    }); 

,然後刪除自定義的行爲,它在.xml文件錨,該CoordinatorLayout的默認行爲處理的小吃吧和onScrollListener的滾動行爲。

<?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:id="@+id/fragment_coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="de.flowment.designExample.StartActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/startRecyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical"> 

    </android.support.v7.widget.RecyclerView> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:layout_anchorGravity="bottom|end" 
     android:src="@android:drawable/ic_dialog_email" /> 

    <!-- DELETE THIS PART, BECAUSE IT'S NOT USED ANYMORE AND BLOCKS THE DEFAULT. 
    app:layout_anchor="@id/startRecyclerView" 
    app:layout_behavior="de.flowment.designExample.FABScrollBehavior" />--> 


</android.support.design.widget.CoordinatorLayout> 

所以我達到了兩個行爲,但就像我說的這是更多的解決方法。

相關問題