The method of realizing sliding distance listener in Android ScrollView _android

Source: Internet
Author: User

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.

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.