androidscrollviewandroid-scrollviewonscrolllisteneronscrollchanged

onScrollChanged firing multiple times for scroll end in scrollView


I have implemented a listener class to detect end of scroll view referring the link 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);
   }
}

Listener is set to scrollView:

scrollView.setBottomReachesListener(new GenericScrollListerner(this));

My GenericScrollListerner class:

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

}

My issue is that onBottomReached is firing twice most of the times. How to handle this issue ???


Solution

  • At last, I found an answer without a tweak.

    Extend Custom scroll view with 'NestedScrollView' instead of 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);
       }
    }
    

    The above code will work