gpt4 book ai didi

android - onScrollChanged 在 scrollView 中多次触发滚动结束

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:31 30 4
gpt4 key购买 nike

我已经实现了一个监听器类来检测引用链接 https://gist.github.com/marteinn/9427072 的 ScrollView 的结尾

public class ResponsiveScrollView extends ScrollView {

private OnBottomReachedListener listener;

public ResponsiveScrollView(Context context) {
super(context);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = getChildAt(getChildCount()-1);
int diff = view.getBottom()-(getHeight()+getScrollY());
if (diff == 0 && listener!= null) {
listener.onBottomReached(this);
}
super.onScrollChanged(l, t, oldl, oldt);
}

public OnBottomReachedListener getBottomChangedListener() {
return listener;
}

public void setBottomReachesListener(OnBottomReachedListener onBottomReachedListener) {
this.listener = onBottomReachedListener;
}

public interface OnBottomReachedListener {
public void onBottomReached(View view);
}
}

监听器设置为 ScrollView :

scrollView.setBottomReachesListener(new GenericScrollListerner(this));

我的 GenericScrollListerner 类:

public class GenericScrollListerner implements ResponsiveScrollView.OnBottomReachedListener {
private Context mContext;

public GenericScrollListerner(Context context) {
this.mContext = context;
}

@Override
public void onBottomReached(View view) {
Log.d("ScrollView","Scroll end");
String tag = (String) view.getTag();
Toast.makeText(mContext, "Scroll end with tag" +tag, Toast.LENGTH_SHORT).show();
}

我的问题是 onBottomReached 大多数时候触发两次。如何处理这个问题???

最佳答案

这是由于过度滚动而发生的。请在 ScrollView xml 声明中添加以下行

android:overScrollMode="never"

如果问题仍然存在,则使用以下调整

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = getChildAt(getChildCount() - 1);
int diff = view.getBottom() - (getHeight() + getScrollY());
if (diff == 0 && listener != null && !stopTwice) {
stopTwice=true;
listener.onBottomReached(this);
}else{
stopTwice=false;
}
super.onScrollChanged(l, t, oldl, oldt);
}

关于android - onScrollChanged 在 scrollView 中多次触发滚动结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42436771/

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