Android Custom ViewGroup scrollable layout gesturedetector gesture monitoring (5) _android

Source: Internet
Author: User

This effect is the same as the previous: Http://www.jb51.net/article/100638.htm effect is the same, but no longer write code in the Ontouchevent, Instead of using the system's own class Gesturedetector to monitor gestures and slide events and so on, it has built-in sliding, click, long press events, and there is a quick slide, more convenient, than the details of their own write better processing.

Code:

Package com.example.libingyuan.horizontallistview.ScrollViewGroup;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.WindowManager;

Import Android.widget.Scroller;
 /** * Custom ViewGroup (horizontal scrolling)/public class Scrollviewgroup extends ViewGroup {//rolling compute auxiliary class private scroller Mscroller;
 Screen width private int screenwidth;
 The maximum distance that can be moved is private int mmaxdistance;
 Custom gesture Listening class private Scrolltouchlisener mtouchlisener;

 Gesture monitoring private gesturedetector mdetector;
 /** * When creating objects using the New keyword, call the/public Scrollviewgroup (context) {This (context, NULL);
 /** * Call/Public scrollviewgroup when used in XML file (context, AttributeSet attrs) {This (context, attrs, 0); /** * is invoked in an XML file, and the custom attribute is used when calling/public Scrollviewgroup (context, AttributeSet attrs, int DefstyleattR) {Super (context, attrs, defstyleattr);
 Init (context); /** * Initialization Method * Initializes the scrolling helper class scroller and calculates the screen width * * * private void init (context context) {//initialization auxiliary class Mscroller = new Sc
  Roller (context);
  Get screen width WindowManager manager = (windowmanager) context. Getsystemservice (Context.window_service);
  Displaymetrics outmetrics = new Displaymetrics ();
  Manager.getdefaultdisplay (). Getmetrics (Outmetrics);
  ScreenWidth = Outmetrics.widthpixels;
  Gesture Indicator Initialization Mtouchlisener = new Scrolltouchlisener ();
 Mdetector = new Gesturedetector (context, mtouchlisener); /** * The method that needs to be overridden when rolling * is used to control scrolling/@Override public void Computescroll () {//To stop if scrolling (mscroller.computescrolloff
   Set ()) {//scroll to the specified position Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());
  This sentence must be written, otherwise not real-time refresh postinvalidate (); 
  }/** * Finger touch screen Event Monitor/@Override public boolean ontouchevent (Motionevent event) {mdetector.ontouchevent (event); if (event.getaction () = = motionevent.action_up) {This. Onup (event);
 return true; /* * Measuring method, measuring the width and height of the parent layout * * * @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//RE Set wide-High this.setmeasureddimension (Measurewidth (Widthmeasurespec, Heightmeasurespec), Measureheight (WidthMeasureSpec,
 HEIGHTMEASURESPEC)); /** * Measuring width */private int measurewidth (int widthmeasurespec, int heightmeasurespec) {//width int sizewidth = M
  Easurespec.getsize (WIDTHMEASURESPEC);
  int modewidth = Measurespec.getmode (Widthmeasurespec);
  The width of the parent control (wrap_content) int width = 0;

  int childcount = Getchildcount ();
   Re-measure the width of the child View and the maximum height for (int i = 0; i < ChildCount i++) {View children = getchildat (i);
   Measurechild (Child, Widthmeasurespec, Heightmeasurespec);
   Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
   int childwidth = child.getmeasuredwidth () + Lp.leftmargin + lp.rightmargin;
  width + + childwidth; return modewidth = = measurespec.exactly?
 Sizewidth:width; }

 /**
  * Measuring Height * * private int measureheight (int widthmeasurespec, int heightmeasurespec) {//height int sizeheight = Measure
  Spec.getsize (HEIGHTMEASURESPEC);
  int modeheight = Measurespec.getmode (Heightmeasurespec);
  The height (wrap_content) int height = 0 of the parent control;

  int childcount = Getchildcount ();
   Re-measure the width of the child View and the maximum height for (int i = 0; i < ChildCount i++) {View children = getchildat (i);
   Measurechild (Child, Widthmeasurespec, Heightmeasurespec);
   Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
   int childheight = child.getmeasuredheight () + Lp.topmargin + lp.bottommargin;
  Height + = childheight;
  Height = Height/childcount; return modeheight = = measurespec.exactly?
 Sizeheight:height; /** * Give sub layout set position * * @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {int childle FT = 0;//to the left of the space int childwidth;//view width int = getheight ();//Screen width int childcount = Getchildcount ();//Child V Number of iew for (int i= 0; i < ChildCount;
   i++) {View child = Getchildat (i);
   Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
   Childwidth = child.getmeasuredwidth () + Lp.leftmargin + lp.rightmargin;
   Child.layout (childleft, 0, Childleft + childwidth, height);
  Childleft + = Childwidth; } @Override Public Layoutparams generatelayoutparams (AttributeSet attrs) {return new Marginlayoutparams (Getcontex
 T (), attrs); /* * Press Event Action_down/public boolean ondown (Motionevent e) {//If stop scrolling, cancel animation (that is, finger press to stop scrolling) if (!mscroller.isfin
  Ished ()) {mscroller.abortanimation ();
 return false; * * * Lift Event ACTION_UP/public boolean onup (Motionevent e) {//Get the Last child view view LastChild = Getchildat (Getchildcou
  NT ()-1); 
  Gets the maximum sliding distance of the slide (the coordinates of the last child's right border minus the screen width) int finalychild = (int) (LASTCHILD.GETX () + lastchild.getwidth ()-screenwidth);
  Mmaxdistance = Finalychild;
  If the sliding distance is less than the leftmost (0) of the first control, the rebound to (0,0) point if (GETSCROLLX () < 0) {scrollto (0, 0);
 } If the sliding distance is greater than the maximum sliding distance, slide to the last child view if (GETSCROLLX () >= finalychild) Scrollto (finalychild, 0);
  Refresh interface invalidate ();
 return false; }/* *action_down, Short Press no move/public void onshowpress (Motionevent e) {}/* Short press Action_down, ACTION_UP/Publi
 C Boolean Onsingletapup (Motionevent e) {return false;  }/* *action_down, slow sliding/public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey)
  {//scrolling Scrollby ((int) Distancex, 0);
 return false; }//Action_down, long press not sliding public void onlongpress (Motionevent e) {}/* *action_down, fast sliding, action_up/public bo  Olean onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy) {mscroller.fling (GETSCROLLX (), 0, (int)
  -velocityx, 0, 0, mmaxdistance, 0, 0);
 return false; /** * Custom Gesture Listener Class * * Private class Scrolltouchlisener implements Gesturedetector.ongesturelistener {//press event @Ov Erride public boolean Ondown (Motionevent e) {return scrollviewgroup.This.ondown (e);
  //Click event @Override public void onshowpress (Motionevent e) {ScrollViewGroup.this.onShowPress (e); 
  //Finger Lift Event @Override public boolean onsingletapup (Motionevent e) {return ScrollViewGroup.this.onSingleTapUp (e);
   }//Scrolling event @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {
  Return ScrollViewGroup.this.onScroll (E1, E2, Distancex, Distancey);
  }//Long by Event @Override public void onlongpress (Motionevent e) {ScrollViewGroup.this.onLongPress (e); }//Sliding event @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {RE
  Turn ScrollViewGroup.this.onFling (E1, E2, Velocityx, Velocityy);

 }
 }

}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.