The realization method of Android ListView Elastic Effect _android

Source: Internet
Author: User

On Android to achieve ListView elastic effect, there are many different methods, online a search, there are many, the following posted in the project often used in two ways to achieve listview elastic effect (basically can be used) for your reference:

The first is relatively simple, easy to understand, only dynamically changing the ListView on the Y axis of the removable distance, the code is as follows:

Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import Android.util.DisplayMetrics; 
Import Android.widget.ListView; 
 /** * Elastic listview. * @author E/public class Flexilistview extends listview{//initial can pull Y-axis away from private static final int Max_y_overscro 
  ll_distance = 100; 
  Contextual environment private context mcontext; 
   
  Actually can pull up and down the distance on the y axis private int mmaxyoverscrolldistance; 
    Public Flexilistview {Super (context); 
    Mcontext = context; 
  Initbouncelistview (); 
    Public Flexilistview (context, AttributeSet attrs) {Super (context, attrs); 
    Mcontext = context; 
  Initbouncelistview (); 
    Public Flexilistview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
    Mcontext = context; 
  Initbouncelistview (); private void Initbouncelistview () {final Displaymetrics metrics = mcontext.getresources (). Getdisplaymetrics ( 
      ); Final float Density = metrics.density; 
  mmaxyoverscrolldistance = (int) (density * max_y_overscroll_distance);  
      @Override protected Boolean Overscrollby (int deltax, int deltay, int scrollx, int scrolly, int scrollrangex, int scrollrangey, int maxoverscrollx, int maxoverscrolly, Boolean istouchevent) {//The essence of the implementation is to dynamically change the MAXOVERSCR here Olly Value return Super.overscrollby (DeltaX, DeltaY, Scrollx, scrolly, Scrollrangex, Scrollrangey, MAXOVERSCROLLX, MMaxYO  
  Verscrolldistance, istouchevent); 
 } 
   
}

The second method, , combines gestures to achieve a ListView effect, where more extensions can be made based on gestures, the code is as follows:

Import Android.content.Context; 
Import Android.graphics.Rect; 
Import Android.util.AttributeSet; 
Import Android.view.GestureDetector; 
Import Android.view.GestureDetector.OnGestureListener; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import android.view.animation.TranslateAnimation; 
Import Android.widget.ListView; /** * ListView with elastic effect. 
 The main is to implement the parent class Dispatchtouchevent method and Ongesturelistener Onscroll method. * @author E */public class Flexiblelistview extends ListView implements ongesturelistener{private Context cont 
  ext = NULL; 
  Private Boolean outbound = false; 
  private int distance; 
   
  private int firstout; 
    Public Flexiblelistview (context, AttributeSet attrs) {Super (context, attrs); 
  This.context = context; Flexiblelistview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle) 
    ; 
  This.context = context; } public Flexiblelistview {super (context); 
  This.context = context; 
   
  } gesturedetector lisgesturedetector = new Gesturedetector (context, this); 
    @Override public boolean dispatchtouchevent (Motionevent event) {int act = event.getaction (); 
    if (act = = motionevent.action_up | | act = = motionevent.action_cancel) && outbound) {outbound = false; 
    Scroll back} if (!lisgesturedetector.ontouchevent (event)) {outbound = false; 
    else {outbound = true;  
    } Rect Rect = new Rect ();  
    Getlocalvisiblerect (rect);  
    translateanimation am = new Translateanimation (0, 0,-rect.top, 0);  
    Am.setduration (300);  
    Startanimation (AM); 
    Scrollto (0, 0); 
  Return Super.dispatchtouchevent (event); 
  @Override public boolean Ondown (Motionevent e) {return false; @Override public void onshowpress (Motionevent e) {} @Override public boolean onsingletapup (Motioneve 
  NT e) {return false; 
 
}  @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {int 
    Firstpos = Getfirstvisibleposition (); 
    int lastpos = Getlastvisibleposition (); 
    int itemCount = GetCount (); 
    Outbound Top if (outbound && firstpos!= 0 && lastpos!= (itemCount-1)) {scrollto (0, 0); 
    return false; 
    View Firstview = Getchildat (Firstpos); 
    if (!outbound) firstout = (int) E2.getrawy (); if (Firstview!= null&& (Outbound | | (firstpos = 0 && firstview.gettop () = 0 && Distancey < 0)) 
    {//record the length of each slide distance = firstout-(int) E2.getrawy (); 
    Scrollto (0, DISTANCE/2); 
    return true; 
  }//Outbound Bottom return false;  The @Override public void onlongpress (Motionevent e) {} is @Override public boolean onfling (Motionevent E1, Motionevent E2, float Velocityx, float velocityy) {return false;  } 
}

The above two commonly used Android ListView elastic Effect of the implementation method, sorted out, hope to help everyone!

Related Article

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.