Android Custom ViewGroup using adapter adapter data (6) _android

Source: Internet
Author: User
Tags int size

On the basis of custom ViewGroup (5): http://www.jb51.net/article/100639.htm, add the use of adapter to fit the data, which is more convenient, just use the adapter adapter data and cannot be updated.

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

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;

  Private Baseadapter Madapter;
  /** * 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 is (context, attrs, 0); /** * is invoked in an XML file, and the custom attribute is used when calling/public Scrollviewgroup (context, AttributeSet attrs, int defstyleat
    TR) {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 of the auxiliary class Mscroller
    = new Scroller (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.compute
      Scrolloffset ()) {//scroll to the specified position Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ()); This sentence must be written, or it cannot be refreshed in real time
      Invalidate (); /** * Finger touchscreen event Monitor/@Override public boolean ontouchevent (Motionevent event) {Mdetector.ontouchevent
    (event);
    if (event.getaction () = = motionevent.action_up) {this.onup (event);
  return true;
    public void Setadapter (Baseadapter adapter) {This.madapter = adapter;
  Requestlayout ();
    /* * Measuring method, measuring the width and height of the parent layout * * * @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
    Removeallviews ();
      for (int i = 0; i < Madapter.getcount (); i++) {View child = Madapter.getview (i, NULL, NULL);
    AddView (child); //Reset Width High this.setmeasureddimension (Measurewidth (Widthmeasurespec, Heightmeasurespec), Measureheight (widthMeasure
  Spec, Heightmeasurespec)); /** * Measuring width */private int measurewidth (int widthmeasurespec, int heightmeasurespec) {//width int Sizew
    Idth = Measurespec.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);
      Layoutparams LP = Child.getlayoutparams ();
      /* Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
      int childwidth = child.getmeasuredwidth () + Lp.leftmargin + lp.rightmargin;
    Width + + childwidth;*/width + + = Child.getmeasuredwidth (); return modewidth = = measurespec.exactly?
  Sizewidth:width; /** * Measuring Height * * private int measureheight (int widthmeasurespec, int heightmeasurespec) {//height int size
    Height = Measurespec.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 child = Getchildat (i);
      Measurechild (Child, Widthmeasurespec, Heightmeasurespec);
      /* Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
      int childheight = child.getmeasuredheight () + Lp.topmargin + lp.bottommargin;
    Height = childheight;*/height = child.getmeasuredheight ();
    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 Childleft = 0;//to the left of the space int childwidth;//view width int height = getheight ()//Screen width int childcount = Getchil
     DCount ();//number of view (int i = 0; i < ChildCount; i++) {View child = Getchildat (i);
      /* Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams (); Childwidth = child.getmeasuredwidth () + Lp.leftmargin + lp.rightmargin;*/childwidth = child.getmeasuredWidth ();
      Child.layout (childleft, 0, Childleft + childwidth, height);
      Childlayout (child);
    Childleft + = Childwidth; }/** * Lets child view's child view and child view size */private void Childlayout (View child) {if (child==null) {throw NE
    W illegalstateexception ("Scrollviewgroup must has one child"); } if (child instanceof ViewGroup) {if ((ViewGroup) child). Getchildcount () >0) throw new Illegalstateexc
      Eption ("View can host only one direct child");
    ((viewgroup) child). Getchildat (0). Layout (0,0,child.getwidth (), Child.getheight ()); } @Override Public Layoutparams generatelayoutparams (AttributeSet attrs) {return new Marginlayoutparams (getc
  Ontext (), attrs); /* * Press Event Action_down/public boolean ondown (Motionevent e) {//Get last child view view LastChild = Getchil
    DAt (Getchildcount ()-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 you stop scrolling, the animation is canceled (that is, the finger presses to stop scrolling) if (!mscroller.isfinished ()) {mscroller.abortanimation ();
  return false; * * * Lift Event ACTION_UP/public boolean onup (Motionevent e) {//If the sliding distance is less than the leftmost (0) of the first control, bounce back to (0,0) point if (GETSCR
    OLLX () <= 0) {scrollto (0, 0);
    //If the sliding distance is greater than the maximum sliding distance, slide to the last child view if (GETSCROLLX () >= mmaxdistance) {scrollto (mmaxdistance, 0);
    //Refresh interface invalidate ();
  return false; }/* *action_down, Short Press no move/public void onshowpress (Motionevent e) {}/* Short press Action_down, ACTION_UP *
  /public boolean onsingletapup (Motionevent e) {return false; }/* *action_down, slow sliding/public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float Distan
    CEY) {//rolling scrollby (int) Distancex, 0);
  return false;
  }//Action_down, long press not sliding public void onlongpress (Motionevent e) {}/* *action_down, fast sliding, action_up/public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {mscroller.fling (
    ), 0, (int)-velocityx, 0, 0, mmaxdistance, 0, 0);
  return false; 
    /** * Custom Gesture Listener Class * * Private class Scrolltouchlisener implements Gesturedetector.ongesturelistener {//press Event
    @Override 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.onSingle
    Tapup (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) {
    Return 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.