我想在滚动时更改工具栏的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的方法:
addOnScrollChangedListener :
scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
// use scroll.getScrollY()
}
});
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的任何其他方式?
答案 0 :(得分:6)
您可以将scrollListener添加到ScrollView
。
滚动视图滚动百分比的计算:
double percent = ((((float) scrollY) / ((float) (scrollContentHeight - screenHeight + statusBarHeight))));
视图显示部分的上边缘:scrollY =
scrollView.getScrollY()
scrollView
有一个孩子,scrollContentHeight =
scrollView.getChildAt(0).getHeight()
是内容的高度
of 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));
}
});
答案 1 :(得分:3)
尝试
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%的百分比
答案 2 :(得分:1)
试试这个方法:
nestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
int verticalScrollableHeight = v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight();
float verticalPercentage = ((float) scrollY) / verticalScrollableHeight;