Forked from marteinn/InteractiveScrollView.java
Last active
February 21, 2023 08:25
-
-
Save mendhak/4792f650dbbe0f11e57b57c2d929bc06 to your computer and use it in GitHub Desktop.
ScrollView with a OnBottomReachedListener for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package se.marteinn.ui; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.ScrollView; | |
/** | |
* Triggers a event when scrolling reaches bottom. | |
* | |
* Created by martinsandstrom on 2010-05-12. | |
* Updated by martinsandstrom on 2014-07-22. | |
* Updated by mendhak from alcntml on 2017-04-17 | |
* | |
* Usage: | |
* | |
* scrollView.setOnBottomReachedListener( | |
* new InteractiveScrollView.OnBottomReachedListener() { | |
* @Override | |
* public void onBottomReached() { | |
* // do something | |
* } | |
* } | |
* ); | |
* | |
* | |
* Include in layout: | |
* | |
* <se.marteinn.ui.InteractiveScrollView | |
* android:layout_width="match_parent" | |
* android:layout_height="match_parent" /> | |
* | |
*/ | |
public class InteractiveScrollView extends ScrollView { | |
private OnBottomReachedListener onBottomReachedListener; | |
private OnTopReachedListener onTopReachedListener; | |
private OnScrolledDownListener onScrolledDownListener; | |
private OnScrolledUpListener onScrolledUpListener; | |
public InteractiveScrollView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
public InteractiveScrollView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public InteractiveScrollView(Context context) { | |
super(context); | |
init(); | |
} | |
private void init() { | |
setFadingEdgeLength(0); | |
setVerticalFadingEdgeEnabled(false); | |
setHorizontalFadingEdgeEnabled(false); | |
setOverScrollMode(ScrollView.OVER_SCROLL_NEVER); | |
} | |
@Override | |
protected void onScrollChanged(int l, int t, int oldl, int oldt) { | |
View view = (View) getChildAt(getChildCount()-1); | |
int diff = (view.getBottom()-(getHeight()+getScrollY())); | |
if (Math.abs(diff) <= 20 && onBottomReachedListener != null) { | |
onBottomReachedListener.onBottomReached(t); | |
} else if (getScrollY() <= 0 && onTopReachedListener != null) { | |
onTopReachedListener.onTopReached(t); | |
} else{ | |
if(t<oldt){ | |
if (onScrolledUpListener != null){ | |
onScrolledUpListener.onScrolledUp(t); | |
} | |
}else if (t>oldt){ | |
if (onScrolledDownListener != null){ | |
onScrolledDownListener.onScrolledDown(t); | |
} | |
} | |
} | |
super.onScrollChanged(l, t, oldl, oldt); | |
} | |
//Getters & Setters | |
public OnBottomReachedListener getOnBottomReachedListener() { | |
return onBottomReachedListener; | |
} | |
public void setOnBottomReachedListener(OnBottomReachedListener onBottomReachedListener) { | |
this.onBottomReachedListener = onBottomReachedListener; | |
} | |
public OnTopReachedListener getOnTopReachedListener() { | |
return onTopReachedListener; | |
} | |
public void setOnTopReachedListener(OnTopReachedListener onTopReachedListener) { | |
this.onTopReachedListener = onTopReachedListener; | |
} | |
public void setOnScrolledDownListener(OnScrolledDownListener onScrolledDownListener){ | |
this.onScrolledDownListener = onScrolledDownListener; | |
} | |
public OnScrolledDownListener getOnScrolledDownListener() { | |
return onScrolledDownListener; | |
} | |
public void setOnScrolledUpListener(OnScrolledUpListener onScrolledUpListener){ | |
this.onScrolledUpListener = onScrolledUpListener; | |
} | |
public OnScrolledUpListener getOnScrolledUpListener() { | |
return onScrolledUpListener; | |
} | |
/** | |
* Event listener. | |
*/ | |
public interface OnBottomReachedListener{ | |
public void onBottomReached(int scrollY); | |
} | |
public interface OnTopReachedListener{ | |
public void onTopReached(int scrollY); | |
} | |
public interface OnScrolledDownListener{ | |
public void onScrolledDown(int scrollY); | |
} | |
public interface OnScrolledUpListener{ | |
public void onScrolledUp(int scrollY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't Work ! other solution !