android--Custom Scrolling ViewGroup

Source: Internet
Author: User

The custom control provides a getadapter and Setadapter interface for setting the view to scroll (the full screen width is displayed by default for each view)

 Public  class scrollerviewgroup extends viewgroup {    Private intScreenHeight;//the phone Screen height    Private intCurrentY;//Record the current current Y position    Private intstartscrolly;//Record the scroll position of the Y-direction when the finger presses the touch event    Private intendscrolly;//Record the scroll position of the Y-direction when the finger lifts the touch event    PrivateAdapter Madapter;//Display adapter    PrivateScroller Mscroller; Public Scrollerviewgroup(context context, AttributeSet attrs) { This(Context,NULL, attrs); } Public Scrollerviewgroup(Context context) { This(Context,NULL,NULL); } Public Scrollerviewgroup(context context, Adapter Adapter) { This(Context, adapter,NULL); } Public Scrollerviewgroup(context context, Adapter Adapter, AttributeSet attrs) { This(context, adapter, Attrs,0); } Public Scrollerviewgroup(context context, Adapter Adapter, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); This. Madapter = Adapter;    Init (context); }//Initialize Mscroller and ScreenHeight    Private void Init(Context context) {Mscroller =NewScroller (context);        Displaymetrics metrics = GetContext (). Getresources (). Getdisplaymetrics ();    ScreenHeight = Metrics.heightpixels; } PublicAdapterGetadapter() {returnMadapter; } Public void Setadapter(Adapter Madapter) { This. Madapter = Madapter; }@Override    protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {Super. Onmeasure (Widthmeasurespec, Heightmeasurespec);if(Madapter = =NULL)return;intCount = Madapter.getcount (); for(inti =0; I < count; i++) {View child = Madapter.getview (i,NULL, This);            AddView (child);        Measurechild (Child, Widthmeasurespec, Heightmeasurespec); }    }@Override    protectedLayoutparamsGeneratelayoutparams(Viewgroup.layoutparams p) {return NewMarginlayoutparams (P); }@Override    protected void OnLayout(BooleanChangedintLintTintRintb) {intCount = Getchildcount ();        Marginlayoutparams MLP = (marginlayoutparams) getlayoutparams ();        Mlp.height = ScreenHeight * count; Setlayoutparams (MLP); for(inti =0, j =0; I < count; i++) {View child = Getchildat (i);if(child.getvisibility () = = VISIBLE)            {Child.layout (L, J * ScreenHeight, R, (++j) * screenheight); }        }    }@Override     Public Boolean ontouchevent(Motionevent event) {inty = (int) event.gety ();Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:currentY = y; startscrolly = getscrolly (); Break; CaseMotionevent.action_move:if(!mscroller.isfinished ()) mscroller.abortanimation ();intoffset = currenty-y;if(Getscrolly () <0) offset =0;if(getscrolly () > GetHeight ()-screenheight) offset =0; Scrollby (0, offset); CurrentY = y; Break; CaseMotionEvent.ACTION_UP:endScrollY = getscrolly ();intOffsetY = endscrolly-startscrolly;if(OffsetY >0) {if(OffsetY > ScreenHeight/3) {Mscroller.startscroll (0, Endscrolly,0, screenheight-offsety); }Else{Mscroller.startscroll (0, Endscrolly,0,-offsety); }                }Else{if(-offsety > ScreenHeight/3) {Mscroller.startscroll (0, Endscrolly,0,-screenheight-offsety); }Else{Mscroller.startscroll (0, Endscrolly,0,-offsety); }                } Break; } postinvalidate ();return true; }@Override     Public void Computescroll() {Super. Computescroll ();if(Mscroller.computescrolloffset ()) {ScrollTo (0, Mscroller.getcurry ());        Postinvalidate (); }    }}

When used:

protected void onCreate(Bundle savedinstancestate)        {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Adapter Adapter =NewArrayadapter<integer> ( This,0, color) {@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {intcolor = GetItem (position);if(Convertview = =NULL) {Convertview =NewView (GetContext ()); Viewgroup.layoutparamsparams=NewViewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, Viewgro Up.                    Layoutparams.match_parent); Convertview.setlayoutparams (params); } convertview.setbackgroundcolor (color);returnConvertview;        }        }; SVG = (scrollerviewgroup) Findviewbyid (r.id.svg);if(svg==NULL) LOG.E ("Main","NULL");    Svg.setadapter (adapter); } integer[] color =NewInteger[]{color.red, Color.Blue, color.yellow};

android--Custom Scrolling ViewGroup

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.