Last article "Custom ViewGroup (2)" Address: http://www.jb51.net/article/100610.htm
Code
Package com.example.libingyuan.horizontallistview.ScrollViewGroup;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.WindowManager;
Import Android.widget.Scroller; /** * Custom ViewGroup * On the basis of scrolling, added boundary limit/public class Scrollviewgroup extends ViewGroup {//scrolling compute auxiliary class private scroller m
Scroller;
The x coordinate of the finger placement is private float Mlastmotionx = 0;
Screen width private int screenwidth;
/** * When using the New keyword to create an object, call * @param context Contexts/public Scrollviewgroup {This (context, NULL); /** * @param context * @param attrs properties when used in XML files: such as android:layout_width= "wrap_content"/public Sc
Rollviewgroup (context, AttributeSet attrs) {This (context, attrs, 0); /** * is invoked in an XML file, and when a custom attribute is used, the * @param context contexts * @param attrs properties: such as android:layout_width= "Wrap_content" * @param DefSTyleattr the ID of the custom attribute/public Scrollviewgroup (context, AttributeSet attrs, int defstyleattr) {Super (context, a
Ttrs, defstyleattr);
Init (context); /** * Initialization Method * Initializes the scrolling helper class scroller and calculates the screen width * @param context */private void init (context context) {Mscroller
= new Scroller (context);
WindowManager manager = (windowmanager) context. Getsystemservice (Context.window_service);
Displaymetrics outmetrics = new Displaymetrics ();
Manager.getdefaultdisplay (). Getmetrics (Outmetrics);
ScreenWidth = Outmetrics.widthpixels; /** * 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 Touchscreen Event Monitor * @param event * @return * * @Override public boolean ontouchevent (Motionevent event) {/
/TODO auto-generated method stub int action = Event.getaction (); float x =Event.getx ();
Switch (action) {Case MotionEvent.ACTION_DOWN:if (!mscroller.isfinished ()) {mscroller.abortanimation ();
} Mlastmotionx = Event.getx ();
Break
Case MotionEvent.ACTION_MOVE:float delt = mlastmotionx-x;
Mlastmotionx = x;
Scrollby ((int) delt, 0);
Break
Case MotionEvent.ACTION_UP:View Lastchild=getchildat (Getchildcount ()-1);
int finalychild= (int) (LASTCHILD.GETX () +lastchild.getwidth ()-screenwidth);
if (GETSCROLLX () <0) {Scrollto (0,0);
} if (Getscrollx () >=finalychild) Scrollto (finalychild,0);
Invalidate ();
Break
Default:break;
return true; @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Reset width High this.setmeasureddimens
Ion (Measurewidth (Widthmeasurespec, Heightmeasurespec), Measureheight (Widthmeasurespec, Heightmeasurespec));
/** * Measuring width */private int measurewidth (int widthmeasurespec, int heightmeasurespec) { width int sizewidth = 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);
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 =
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 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 (GetContext (), attrs);
}
}
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.