Custom ScrollView for ScrollView sliding monitoring and recording of sliding positions.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.