簡體   English   中英

當ScrollView或RecyclerView Z索引較小時,在底部視圖上顯示陰影高程 - Android Material Design

[英]Show shadow elevation on bottom view when a ScrollView or RecyclerView Z index is smaller - Android Material Design

我過去常常在我的工具欄視圖下面強制陰影,以獲得更好的向后支持,如下所示:

<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" />

@繪制/ 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>

在此輸入圖像描述

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

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

問題是,我不想讓底部陰影始終可見,我想只在滾動視圖位於底部布局“下方”時才顯示“底部陰影” ,並討論Z索引。

換句話說,當滾動視圖底部到達底部布局頂部時,我需要顯示底部陰影。

這是底部視圖中沒有陰影的布局:

在此輸入圖像描述

我一直在考慮在代碼上執行此操作,檢查Y索引視圖以及它們是否相同,這意味着底部布局需要比Scrollview具有更高的高度/ translationZ,但我不確定這是否是最好的選擇,我想也許有一種方法可以正確設置我的布局。

有任何想法嗎?

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

對於那些有需要的人來說,到目前為止它一直很好用。

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);
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM