gpt4 book ai didi

android - 检测 ScrollView 的顶部

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:25 25 4
gpt4 key购买 nike

如何检测 ScrollView 的顶部?我已经实现了检测底部的代码,但我似乎无法弄清楚确定何时达到顶部的计算。

     @Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
int startPosition = t;
View view = (View) getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY()));

if (diff == startPosition){
oListener.onTopReached();
} else {
nListener.BottomNotReached();
}

/* if (diff == 0 && mListener != null) {
mListener.onBottomReached();
} else if (diff == getTop()) {
oListener.onTopReached();
} else {
nListener.BottomNotReached();
}*/

super.onScrollChanged(l, t, oldl, oldt);
}

最佳答案

作为Marteinn suggests :

 @Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {

if(view.getTop()==t){
// reaches the top end
}

View view = (View) getChildAt(getChildCount()-1);
int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));// Calculate the scrolldiff
if( diff <= 0 ){
// if diff is zero, then the bottom has been reached
Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
}
super.onScrollChanged(l, t, oldl, oldt);
}

关于android - 检测 ScrollView 的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com