OnScrollChanged fires several times to scroll in scrollView

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

The 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 problem is that onBottomReached starts twice in most cases. How to deal with this problem?

+6
source share
2 answers

Finally, I found the answer without a hint.

Extending 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

+1
source

This is due to over scrolling. Add the following line to the scrollview xml declaration

 android:overScrollMode="never" 

If the problem still persists, use the following setting

 @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); } 
+4
source

Source: https://habr.com/ru/post/1015245/


All Articles