Last article Custom ViewGroup (1) Address: http://www.jb51.net/article/100608.htm
Here the Direct code:
Package com.example.libingyuan.horizontallistview.ScrollViewGroup;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Scroller; /** * Custom ViewGroup * On the basis of horizontal layout, increased scrolling effect, but no boundary restrictions/public class Scrollviewgroup extends ViewGroup {private scroller
Mscroller;
private float Mlastmotionx = 0;
Public Scrollviewgroup {This (context, NULL);
Public Scrollviewgroup (context, AttributeSet attrs) {This (context, attrs, 0); Scrollviewgroup (context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyleatt
R);
Init (context);
private void init (context context) {Mscroller = new scroller (context); @Override public void Computescroll () {if (Mscroller.computescrolloffset ()) {Scrollto (Mscroller.getcurr
X (), Mscroller.getcurry ());
Postinvalidate (); @Override public boolean ontouchevent (Motionevent event) {//TODO auto-generated method stub int A
ction = Event.getaction ();
float x = Event.getx (); Switch (action) {Case MotionEvent.ACTION_DOWN:if (!mscroller.isfinished ()) {Mscroller.abortanim
Ation ();
} Mlastmotionx = Event.getx ();
Break
Case MotionEvent.ACTION_MOVE:float delt = mlastmotionx-x;
Mlastmotionx = x;
Scrollby ((int) delt, 0);
Break
Case MotionEvent.ACTION_UP:invalidate ();
Break
Default:break;
return true; @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Reset width High this.setmeasured
Dimension (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 Sizeh
eight = 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 = Height/childcount; return modeheight = = measurespec.exactly?
Sizeheight:height;
@Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {int childleft = 0;
int childwidth;
int height = getheight ();
int childcount = Getchildcount ();
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.