1, inherit Horizontalscrollview, mainly use its characteristics Scrollto and Smoothscrollto method of horizontal scrolling
2, the Almighty Ontouchevent
Public classSlidingmenuextendshorizontalscrollview{/*** Screen width*/ Private intMscreenwidth; /*** DP*/ Private intmmenurightpadding; /*** Width of menu*/ Private intMmenuwidth; Private intMhalfmenuwidth; Private BooleanIsOpen; Private Booleanonce; PublicSlidingmenu (Context context, AttributeSet attrs) { This(context, Attrs, 0); } PublicSlidingmenu (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Mscreenwidth=screenutils.getscreenwidth (context); TypedArray a=context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.slidingmenu, Defstyle,0); intn =A.getindexcount (); for(inti = 0; I < n; i++) { intattr =A.getindex (i); Switch(attr) { Caser.styleable.slidingmenu_rightpadding://defaultMmenurightpadding =a.getdimensionpixelsize (attr, (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 50f, Getresources (). Getdisplaymetrics ( ));//default is 10DP Break; }} a.recycle (); } PublicSlidingmenu (Context context) { This(Context,NULL, 0); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { /*** Display the setting of a width*/ if(!once) {LinearLayout wrapper= (linearlayout) getchildat (0); ViewGroup Menu= (viewgroup) wrapper.getchildat (0); ViewGroup content= (ViewGroup) wrapper.getchildat (1); Mmenuwidth= Mscreenwidth-mmenurightpadding; Mhalfmenuwidth= MMENUWIDTH/2; Menu.getlayoutparams (). Width=Mmenuwidth; Content.getlayoutparams (). Width=Mscreenwidth; } Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } @Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {Super. OnLayout (Changed, L, T, R, b); if(changed) {//Hide the menu This. ScrollTo (mmenuwidth, 0); Once=true; }} @Override Public Booleanontouchevent (motionevent ev) {intAction =ev.getaction (); Switch(action) {//Up , if the display area is greater than half the width of the menu, it is completely displayed, otherwise hidden Casemotionevent.action_up:intSCROLLX =Getscrollx (); if(Scrollx >mhalfmenuwidth) { This. Smoothscrollto (mmenuwidth, 0); IsOpen=false; } Else { This. Smoothscrollto (0, 0); IsOpen=true; } return true; } return Super. ontouchevent (EV); } /*** Open Menu*/ Public voidOpenmenu () {if(IsOpen)return; This. Smoothscrollto (0, 0); IsOpen=true; } /*** Close Menu*/ Public voidClosemenu () {if(isOpen) { This. Smoothscrollto (mmenuwidth, 0); IsOpen=false; } } /*** Toggle Menu Status*/ Public voidToggle () {if(IsOpen) {closemenu (); } Else{openmenu (); } }}
Android Custom slide-up panel