Android Horizontalscrollview for scrolling status monitoring

Source: Internet
Author: User


Most of the online is directly called onscrollchanged (int x, int y, int oldx, int oldy) This method, in fact, only the protected of this method to public only, essentially there is no much help, not many said, Directly on the code



Package Com.dzc.gallery;import Android.content.context;import Android.os.handler;import android.util.AttributeSet; Import Android.util.log;import Android.view.motionevent;import Android.widget.horizontalscrollview;public class Scrolllistenerhorizontalscrollview extends Horizontalscrollview{public Scrolllistenerhorizontalscrollview (Context Context,attributeset attrs, int defstyle) {Super (context, attrs, defstyle);//TODO auto-generated constructor stub} Public Scrolllistenerhorizontalscrollview (Context Context,attributeset attrs) {Super (context, attrs);//TODO Auto-generated Constructor Stub}public Scrolllistenerhorizontalscrollview (context context) {super (context);//TODO    Auto-generated constructor Stub}public interface Scrollviewlistener {void onscrollchanged (Scrolltype scrolltype);         } private Handler mhandler;private scrollviewlistener scrollviewlistener;/** * Scroll State idle scroll stop touch_scroll finger drag scroll Fling Scroll * @version xhorizontalscrollviewgallery * @author DZC * @Time 2014-12-7 a.m. 11:06:52 * * * * */enum scrolltype{idle,touch_scroll,fling};    /** * Record the current scrolling distance */private int currentx =-9999999;    /** * Current scrolling status */private Scrolltype scrolltype = Scrolltype.idle;    /** * Scrolling Listening interval */private int scrolldealy = 50; /** * Scrolling Monitor Runnable */private Runnable scrollrunnable = new Runnable () {@Overridepublic void Run () {//TODO Aut O-generated Method Stubif (GETSCROLLX () ==currentx) {//Scroll stop cancels the listener thread log.d ("", "Stop Scrolling"); Scrolltype = Scrolltype.idle;if ( Scrollviewlistener!=null) {scrollviewlistener.onscrollchanged (scrolltype);} Mhandler.removecallbacks (this); return;} else{//fingers leave the screen view while scrolling log.d ("", "Fling ... Scrolltype = Scrolltype.fling;if (scrollviewlistener!=null) {scrollviewlistener.onscrollchanged (ScrollType);}} CurrentX = Getscrollx (); mhandler.postdelayed (this, scrolldealy);}}; @Overridepublic boolean ontouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_move: This.scrolltype = scrolltype.tOuch_scroll;scrollviewlistener.onscrollchanged (Scrolltype);//Remove the scrolling listener thread mhandler.removecallbacks when the finger moves on it ( scrollrunnable); Break;case motionevent.action_up://when the finger moves mhandler.post (scrollrunnable); return super.ontouchevent (EV);} /** * Must call this method to set handler or it will go wrong * 2014-12-7 pm 3:55:39 * @author DZC * @return void * @param handler * @TODO */public V OID SetHandler (Handler Handler) {this.mhandler = Handler;} /** * Set Scrolling monitoring * 2014-12-7 pm 3:59:51 * @author DZC * @return void * @param listener * @TODO */public void Setonscrollsta Techangedlistener (Scrollviewlistener listener) {This.scrollviewlistener = Listener;}}


Code comments have been written I think very detailed, do not understand can leave a message, say the idea, is to open a thread, each time interval to determine whether the current SCROLLX change, if there is a change in the scroll, if there is no change in the description has stopped rolling.

One thing to note is that when you use it, you must call SetHandler (Handler Handler) to pass in an activity-created Handler, or it will go wrong.

You may wonder if it will affect the performance of this thread, in fact, this is not to be careful, almost no impact

Android Horizontalscrollview for scrolling status monitoring

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.