【问题标题】:onScrollChanged firing multiple times for scroll end in scrollViewonScrollChanged 在scrollView中多次触发滚动结束
【发布时间】:2017-07-15 04:12:20
【问题描述】:

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

}

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

【问题讨论】:

    标签: android scrollview android-scrollview onscrolllistener onscrollchanged


    【解决方案1】:

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

    【讨论】:

    • 哪一个有效? xml 声明或调整? @remyathekkuvettil
    • 这应该是认可的答案
    【解决方案2】:

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

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

    上面的代码可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 2022-01-06
      相关资源
      最近更新 更多