Objective
It is well known that ScrollView is a UI control that we often use, and you may find in the process of using scrollview that you do not have the right listener when you want to monitor the distance ScrollView sliding! Of course, there are APIs available in API 23 setOnScrollChangeListener(View.OnScrollChangeListener l) that are not compatible with the low version. What about it? Had to rewrite the scrollview to achieve the monitoring of the sliding distance.
Say not much, directly on the code:
public class Myscrollview extends ScrollView {
private onscrolllistener listener;
/**
* Set sliding distance listener
/public void Setonscrolllistener (Onscrolllistener listener) {
This.listener = Listener;
}
Public Myscrollview {
super (context);
}
Public Myscrollview (context, AttributeSet attrs) {
Super (context, attrs);
}
Public Myscrollview (context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr); c16/>}
///sliding distance listener public
interface onscrolllistener{
/**
* Called when sliding, scrolly for sliding distance
* *
void onscroll (int scrolly);
}
@Override public
void Computescroll () {
super.computescroll ();
if (listener!=null) {
listener.onscroll (getscrolly ());}}}
The above rewrite of the Myscrollview is in the computeScroll() implementation of listening, because the ScrollView internal is through the scroller to achieve, when the sliding will call computeScroll() methods, so as to achieve the effect of listening.
Of course there is another way, is onScrollChanged(int l, int t, int oldl, int oldt) to listen, the final effect is the same:
public class Myscrollview extends ScrollView {
private onscrolllistener listener;
public void Setonscrolllistener (Onscrolllistener listener) {
This.listener = listener;
}
Public Myscrollview {
super (context);
}
Public Myscrollview (context, AttributeSet attrs) {
Super (context, attrs);
}
Public Myscrollview (context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr); c14/>} public
interface onscrolllistener{
void onscroll (int scrolly);
}
@Override
protected void onscrollchanged (int l, int t, int oldl, int Oldt) {
super.onscrollchanged (L, T, OLDL, O LDT);
if (listener!= null) {
listener.onscroll (t);}}}
Summarize
This is the full content of this article, I hope you can help Android developers, if you have questions you can leave a message exchange.