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 ???
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);
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With