2016-04-05 32 views
3

我用來強制低於我的工具欄視圖陰影效果更好的支持向後這樣的:顯示陰影高程時,滾動型或RecyclerView Z指數較小 - Android電子材料設計

<View 
      android:id="@+id/toolbar_shadow" 
      android:layout_width="match_parent" 
      android:layout_height="4dp" 
      android:layout_below="@+id/toolbar" 
      android:background="@drawable/shadow_elevation" /> 

@drawable/shadow_elevation

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient 
     android:angle="90" 
     android:endColor="#12000000" 
     android:startColor="@android:color/transparent" /> 
</shape> 

enter image description here

現在我需要做同樣的效果,但在底部是這樣的

<--Toolbar--> 
<--toolbar shadow--> 
<--Scroll View--> 
<--bottom shadow--> 
<--Bottom Layout--> 

的問題是,我不想讓底部陰影始終可見,我想顯示「底部陰影」 只有當滾動視圖是「自下而上」的底部佈局,談論Z指標。

換句話說,我需要的是當scrollview底部碰到底部佈局頂部時顯示底部陰影。

這是一個沒有上底觀影的佈局:

enter image description here

我一直在想這樣做的代碼,檢查的Y指數,如果他們是他們相同的,這就是意味着,底部佈局需要比Scrollview更高的高程/轉換Z,但我不確定這是否是最佳選擇,我認爲也許有一種方法可以正確設置我的佈局。

任何想法?

回答

0

我找到了一個方法!我完全忘了把它放在這裏

對於那些有需要的人來說,迄今爲止工作得很好。

public void setBottomLayoutElevation(View scrollView, View bottomView) { 

    if (scrollView instanceof NestedScrollView) { 
     NestedScrollView nestedScrollView = (NestedScrollView) scrollView; 
     nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() { 
      @Override 
      public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { 

       int max = nestedScrollView.getChildAt(0).getHeight() - nestedScrollView.getHeight(); 

       if (v.canScrollVertically(1)) { 
        int abs = Math.abs((scrollY * 100/max) - 100); 
        ViewCompat.setElevation(bottomView, Math.min(abs, 40)); 
       } else { 
        ViewCompat.setElevation(bottomView, 0); 
       } 
      } 
     }); 
    } 

    if (scrollView instanceof RecyclerView) { 
     ((RecyclerView) scrollView).addOnScrollListener(new RecyclerView.OnScrollListener() { 
      @Override 
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
       ViewCompat.setElevation(bottomView, recyclerView.canScrollVertically(1) ? 30 : 0); 
      } 
     }); 
    } 
}