<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ><span style= "FONT-SIZE:14PX;" > Because ScrollView itself does not have a listener callback event with Onscrolllistener, the first thing to do is rewrite ScrollView, which gives code </span></span>
<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ><span style= "FONT-SIZE:14PX;" >MyScrollView.java</span></span>
<span style= "FONT-SIZE:14PX;" >import Android.content.context;import Android.os.handler;import Android.util.attributeset;import Android.view.motionevent;import Android.widget.scrollview;public class Myscrollview extends ScrollView {private OnSc Rolllistener Onscrolllistener; Use the user's finger to leave the Myscrollview,myscrollview still continue to slide, used to save y distance private int lastscrolly; Public Myscrollview (Context context) {This (context, NULL); } public Myscrollview (context context, AttributeSet Attrs) {This (context, attrs, 0); } public Myscrollview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle) ; }//write a Onscrolllistener listener callback method public void Setonscrolllistener (Onscrolllistener onscrolllistener) {this. Onscrolllistener = Onscrolllistener; }//used to get the y distance of Myscrollview scrolling when the user fingers leave Myscrollview, then callback to Onscroll method in private Handler Handler = new Handler () { Public VoiD handlemessage (android.os.Message msg) {int scrolly = MyScrollView.this.getScrollY (); The distance at this time is not equal to the distance recorded, sending a message to handler in 5 milliseconds if (lastscrolly! = scrolly) {lastscrolly = scrolly ; Handler.sendmessagedelayed (Handler.obtainmessage (), 5); } if (Onscrolllistener! = null) {Onscrolllistener.onscroll (scrolly); } }; }; /** * Rewrite ontouchevent, when the user's hand is above the Myscrollview, * direct the Myscrollview sliding y-direction distance back to the Onscroll method, when the user raises his hands, * Myscrol LView may still be sliding, so when the user lifts his hand we send a message to handler in 5 milliseconds, the distance in handler processing * myscrollview sliding */@Override public boolean OnTo Uchevent (motionevent ev) {if (Onscrolllistener! = null) {onscrolllistener.onscroll (lastscrolly = Thi S.getscrolly ()); } switch (Ev.getaction ()) {Case MotionEvent.ACTION_UP:handler.sendMessageDelayed (HandlEr.obtainmessage (), 5); Break } return super.ontouchevent (EV); }//Scrolling callback Interface public interface onscrolllistener{//return the sliding y distance from public void onscroll (int scrolly); }} </span>Here are some of the code in the main program
First, add the Myscrollview control to the XML file
<span style= "FONT-SIZE:14PX;" ><com.scnu.yxp.travelapp.view.myscrollview <span style= "White-space:pre" ></span>android: Id= "@+id/scrollview" <span style= "White-space:pre" ></span>android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:background= "#fff"/></span>
The second step is to write a class that inherits the callback interface inside the Myscrollview
<span style= "FONT-SIZE:14PX;" >class Onmyscrolllistener implements com.scnu.yxp.travelapp.view.myscrollview.onscrolllistener{@Overridepublic void onscroll (int scrolly) {/////when the distance of the slide is greater than how much when the corresponding action is performed if (scrolly >= homepage_btn_window.getheight ()) { Upbtn.setvisibility (view.visible);} Else{upbtn.setvisibility (View.gone);}}} </span>
Step three, set up the listener
<span style= "FONT-SIZE:14PX;" ><span style= "White-space:pre" ></span>onmyscrolllistener listener = new Onmyscrolllistener (); Myscrollview.setonscrolllistener (Listener);</span>
I'll be right here.
The last picture below
Android ListView Slide appears back to the top button