Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ???

like image 807
remya thekkuvettil Avatar asked Feb 24 '17 10:02

remya thekkuvettil


2 Answers

This happens due to overscroll. Please add the following line in scrollview xml declaration

android:overScrollMode="never"

If issue still persist then use the following tweak

@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);
    }
like image 160
Unnikrishnan Avatar answered Nov 11 '22 03:11

Unnikrishnan


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

like image 34
remya thekkuvettil Avatar answered Nov 11 '22 02:11

remya thekkuvettil