onscrollchanged在scollview中多次发射滚动端



我已经实现了一个侦听器类来检测滚动视图的末端参考链接https://gist.github.com/marteinn/9427072

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

}

我的问题是,大部分时间都在开火两次。如何处理这个问题???

这是由于超频而发生的。请在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);
    }

终于找到了一个没有调整的答案。

使用" NestedScrollview"而不是scrollview

扩展自定义滚动视图
public class ResponsiveScrollView extends NestedScrollView {
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);
   }
}

上述代码将起作用

最新更新