Android: Custom Horizontalscrollview implementation QQ side-Slide Menu

Source: Internet
Author: User

Today, we saw the qq5.0 side-by-side menu in the Mu-shen network. Learned a lot of knowledge, but also admire the clear idea of the great God.

See the Tutorial class also realized a bit. The code is almost identical don't squirt me. No way O (︶^︶) o ay

Pixel bad no way to find a good GIF production software.

For the moment, we'll call the left side of the menu, the content on the right

The first is the layout of the menu

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" @drawable/img _frame_background "> <linearlayout android:layout_width=" fill_parent "android:layout_height=" Wrap_c            Ontent "android:layout_centerinparent=" true "android:orientation=" vertical "> <relativelayout                 Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" > <imageview Android:id= "@+id/image1" android:layout_width= "50DP" android:layout_height= " 50DP "android:layout_marginleft=" 20DP "android:src=" @drawable/img_1 "/> <te                Xtview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_centerinparent="True" android:layout_torightof= "@id/image1" android:textcolor= "#ffffff" Android oid:layout_marginleft= "20DP" android:text= "first item" android:textsize= "20sp"/> &lt ;/relativelayout> <relativelayout android:layout_width= "Wrap_content" android:layout_he ight= "Wrap_content" > <imageview android:id= "@+id/image2" Android:layout_wi Dth= "50DP" android:layout_height= "50DP" android:layout_marginleft= "20DP" Android                oid:src= "@drawable/img_2"/> <textview android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerinparent= "true" Android:layout_tor                ightof= "@id/image2" android:textcolor= "#ffffff" android:layout_marginleft= "20DP" Android:text= "a second iteM "android:textsize=" 20sp "/> </RelativeLayout> <relativelayout Androi                D:layout_width= "Wrap_content" android:layout_height= "wrap_content" > <imageview                Android:id= "@+id/image3" android:layout_width= "50DP" android:layout_height= "50DP"                android:layout_marginleft= "20DP" android:src= "@drawable/img_3"/> <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layo                Ut_centerinparent= "true" android:layout_torightof= "@id/image3" android:textcolor= "#ffffff" android:layout_marginleft= "20DP" android:text= "third item" android:textsize= "20SP            "/> </RelativeLayout> <relativelayout android:layout_width=" Wrap_content " Android:layout_height= "Wrap_content "> <imageview android:id=" @+id/image4 "android:layout_width=" 5 0DP "android:layout_height=" 50DP "android:layout_marginleft=" 20DP "ANDROID:SR c= "@drawable/img_4"/> <textview android:layout_width= "Wrap_content" Androi d:layout_height= "Wrap_content" android:layout_centerinparent= "true" Android:layout_torightof = "@id/image4" android:textcolor= "#ffffff" android:layout_marginleft= "20DP" an             droid:text= "Fourth item" android:textsize= "20sp"/> </RelativeLayout> <relativelayout Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" > <imagev Iew android:id= "@+id/image5" android:layout_width= "50DP" Android:layout_heigh                t= "50DP"android:layout_marginleft= "20DP" android:src= "@drawable/img_5"/> <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_cen                Terinparent= "true" android:layout_torightof= "@id/image5" android:textcolor= "#ffffff" android:layout_marginleft= "20DP" android:text= "fifth item" android:textsize= "20SP"/&gt        ; </RelativeLayout> </LinearLayout></RelativeLayout>

Then is the main layout, a horizontal scroll bar, put into the menu.xml, and then below is a linear vertical layout, the background is the QQ picture

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "> <com.example.myhorizontal ScrollView. Myhorizontalscrollview android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Androi d:scrollbars= "None" > <linearlayout android:layout_width= "wrap_content" Android:layout_h eight= "fill_parent" android:orientation= "horizontal" > <include layout= "@layout/me Nu "/> <linearlayout android:layout_width=" Fill_parent "Android:layout_heig ht= "Fill_parent" android:background= "@drawable/qq"/> </LinearLayout> </com.example.m Yhorizontalscrollview. Myhorizontalscrollview></relativelayout>

The horizontal scroll bar is our custom view

You need to rewrite two of these methods

@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {//TODO auto-generated method stubsuper.onl Ayout (changed, L, T, R, b);}
Adjust our initial layout by setting the offset, make menu all hidden, right menu appears

@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated method Stubsuper.onmeasure (Widthmeasurespec, heightmeasurespec);}
Set the width of the child view

* Because Horizontalscrollview himself controls the move and down events
* So we have to judge up by Ontouchevent. If the current x offset is greater than half the width of the menu
* Hide menu, otherwise show menu display by Smoothscrollto (x, Y) method to achieve the effect of animation

Here are all the custom Horizontalscrollview

Package Com.example.myhorizontalscrollview;import Android.annotation.suppresslint;import Android.content.Context; Import Android.text.getchars;import Android.util.attributeset;import Android.util.displaymetrics;import Android.util.typedvalue;import Android.view.motionevent;import Android.view.viewgroup;import Android.view.windowmanager;import Android.widget.horizontalscrollview;import Android.widget.LinearLayout;public Class Myhorizontalscrollview extends Horizontalscrollview {//scroll bar horizontal-first layout private linearlayout mwrpper;// Horizontal layout left menu Menuprivate viewgroup mmenu;//Horizontal First layout right linear layout private viewgroup mcontent;//screen wide private int mscreenwidth;// The width of the menu is from the right side of the screen private int mmenurightpadding=50;//menu, private int Mmenuwidth;private boolean once;/** * Called when no custom attribute is used * */public Myhorizontalscrollview (Context context, AttributeSet Attrs) {Super (context, attrs);/* * Get the width of the screen * Through the context to get WindowManager, in through WindowManager to get metrics, with displaymetrics receive * * */WindowManager WM = (WindowManager) Context.getsystemservice (Context.window_service);D isplaymetrics outmetrics = new Displaymetrics (); Wm.getdefaultdisplay (). GetMetrics ( Outmetrics) mscreenwidth=outmetrics.widthpixels;//convert DP to pxmmenurightpadding= (int) typedvalue.applydimension ( Typedvalue.complex_unit_dip, 50,context.getresources (). Getdisplaymetrics ());} /* * Set the width and height of the child view * */@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generate D method Stubif (!once) {mwrpper= (linearlayout) getchildat (0); mmenu= (viewgroup) mwrpper.getchildat (0); Mcontent= ( ViewGroup) Mwrpper.getchildat (1); the width of the//menu is equal to the width of the screen minus the margin Mmenuwidth=mmenu.getlayoutparams () from the right side of the screen. width= mscreenwidth-mmenurightpadding;//the width of the antecedent layout to the right is directly equal to the width of the screen mcontent.getlayoutparams (). Width=mscreenwidth;once=true;} Super.onmeasure (Widthmeasurespec, heightmeasurespec);} /* * Hide the menu by setting the offset * */@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {//TODO Auto-genera Ted Method Stubsuper.onlayout (changed, L, T, R, b);/* * Set the screen offset by the Scrollto (x, Y) method, X is positive * Content Moves left * */if (changed) {This.scrollto (mmenuwidth, 0);}} /* * Because Horizontalscrollview own control of move and down events * so we have to judge up. If the current x offset is greater than half of the menu width * Hide the menu, the menu * */@Overridepublic Boolean ontouchevent (motionevent ev) {//TODO auto-generated method Stubint action=ev.getaction (); switch (action) {case MotionEvent.ACTION_UP:int scrollx=getscrollx (); if (SCROLLX&GT;=MMENUWIDTH/2) {this.smoothscrollto (mmenuwidth, 0);} Else{this.smoothscrollto (0, 0);} return true;} return super.ontouchevent (EV);}}

And then it's mainactivity loading the layout to

Package Com.example.slipping;import Com.example.helloworld.r;import Android.os.bundle;import android.app.Activity; Import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);}}






Android: Custom Horizontalscrollview implement QQ side-by-side menu

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.