11

我想基于滚动更改工具栏的 alpha,如下所示:

在此处输入图像描述

起初,工具栏是透明的,通过滚动到底部,它会越来越明显,最后它会完全不透明(可见)。

我的布局结构是:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_collapseMode="parallax">

            ....

        </RelativeLayout>

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

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

    <android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

         ....

    </android.support.v4.widget.NestedScrollView>

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

嵌套滚动视图的内容将从服务器动态更改,所以我不知道它的高度。

我刚刚找到了两种检测scrollY的方法:

  1. addOnScrollChangedListener

    scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
     @Override
     public void onScrollChanged() {
        // use scroll.getScrollY()     
    }
    });
    
  2. setOnScrollChangeListener

    scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    
    }
    });
    

但这些方式都不是一帆风顺的。例如,如果您检查scrollY(通过使用log.d())的值,您将看到如下内容:

scrollY: 58
scrollY: 117
scrollY: 167
scrollY: 192
scrollY: 195
scrollY: 238
scrollY: 281
scrollY: 338 

数字之间有很大的差距。

我的问题是:如何(顺利)获得每一刻的滚动百分比?或者根据当前滚动位置更改工具栏 alpha 的任何其他方式?

4

4 回答 4

7

您可以将 scrollListener 添加到ScrollView.

scrollView 滚动百分比的计算:

double percent = ((((float) scrollY) / ((float) (scrollContentHeight - screenHeight + statusBarHeight))));

在此处输入图像描述


  • 视图显示部分的上边缘:scrollY = scrollView.getScrollY()

  • scrollView一个孩子,scrollContentHeight = scrollView.getChildAt(0).getHeight()是 scrollView 内容的高度。

  • 我们需要计算完全滚动状态下上边缘的最大高度:scrollContentHeight - screenHeight + statusBarHeight

  • 完成的!percent = scrollY*100 / (scrollContentHeight - screenHeight + statusBarHeight)

  • 并设置颜色:view.setBackgroundColor(Color.argb((int) (255.0 * (percent/100)), r, g, b));


scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            int scrollY = scrollView.getScrollY();
            int scrollContentHeight = scrollView.getChildAt(0).getHeight();
            int screenHeight = Utility.getScreenHeight(context);
            int statusBarHeight = Utility.getStatusBarHeight(context);

            int color = getResources().getColor(R.color.colorPrimary);
            int r = (color >> 16) & 0xFF;
            int g = (color >> 8) & 0xFF;
            int b = (color >> 0) & 0xFF;

            double percent = ((((float) scrollY) / ((float) (scrollContentHeight - screenHeight + statusBarHeight))));
            if (percent >= 0 && percent <= 1)
                toolbar.setBackgroundColor(Color.argb((int) (255.0 * percent), r, g, b));
        }
    });
于 2017-07-17T11:31:27.027 回答
7

尝试这个

scrollview.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                double scrollViewHeight = scrollview.getChildAt(0).getBottom() - scrollview.getHeight();
                double getScrollY = scrollview.getScrollY();
                double scrollPosition = (getScrollY / scrollViewHeight) * 100d;
                Log.i("scrollview", "scroll Percent Y: " + (int) scrollPosition);
            }
        });

它将显示从 0 到 100% 的百分比

于 2018-12-07T21:17:36.667 回答
2

试试这个方法:

    nestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
                int verticalScrollableHeight = v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight();
                float verticalPercentage = ((float) scrollY) / verticalScrollableHeight;
于 2020-12-23T08:13:42.633 回答
0

这些答案忽略了滚动视图上任何顶部或底部填充的影响,这实际上是额外的滚动跨度,所以我发现我需要:

val verticalScrollableHeight = firstChild.measuredHeight - scrollView.measuredHeight + scrollView.paddingBottom + scrollView.paddingTop
        listener(scrollView.scrollY.toFloat() / verticalScrollableHeight)
于 2021-09-30T09:22:25.000 回答