Android ScrollView for the sliding monitoring interface is not open, however, in many cases to record the sliding position of scrollview, the implementation of this function is relatively simple, the implementation of a Observescrollview class to inherit ScrollView, Then rewrite the inside of the onscrollchanged (int l, int t, int oldl, int Oldt) method, this method is the scrollview of the sliding monitoring, and then declare an interface, in the overridden method using the interface callback, the sliding data out.
onscrollchanged (int l, int t, int oldl, int Oldt) method 4 parameters to the upper left corner of the screen is (0,0) point, L represents the sliding x value, OLDL represents the x position before the slide, T is the Y value after sliding, Oldt indicates the Y position before sliding.
Directly below the code:
public class Observescrollview extends ScrollView {private Scrolllistener mlistener; public static interface Scrolllistener {//Declaration interface for passing data public void scrolloritention (int l, int t, int oldl, int oldt);} Public Observescrollview (context context, AttributeSet Attrs,int Defstyle) {Super (context, attrs, defstyle);//TODO Auto-generated Constructor Stub}public Observescrollview (context context, AttributeSet Attrs) {Super (context, attrs); /Todo auto-generated constructor stub}public Observescrollview (context context) {super (context);//Todo auto-generated constructor stub} @Overrideprotected void Onscrollchanged (int l, int t, int oldl, int Oldt) {//TODO auto-generated method Stubsuper.onscrollchanged (L, T, OLDL, Oldt); if (mlistener! = null) {mlistener.scrolloritention (L, T, OLDL, Oldt);}} public void Setscrolllistener (Scrolllistener l) {this.mlistener = l;}}
Then replace the ScrollView with the overridden Observescrollview in the layout file and set the listener in the code.
Scrollview.setscrolllistener (New Scrolllistener () {@Overridepublic void scrolloritention (int l, int t, int oldl, int Oldt {//TODO auto-generated method stub//Sliding data has been received, where you implement your function}});
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Custom ScrollView for ScrollView sliding monitoring and recording of sliding positions.