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

时间:2017-02-24 10:50:57

标签: android scrollview android-scrollview onscrolllistener onscrollchanged

我已经实现了一个侦听器类来检测链接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));

My 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在大多数情况下都会触发两次。如何处理这个问题???

2 个答案:

答案 0 :(得分:6)

这是由于过度滚动而发生的。请在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);
    }

答案 1 :(得分:1)

最后,我找到了一个没有调整的答案。

使用'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);
   }
}

以上代码可以使用