Full detailed example of the Android slide menu (hardcover version)

Source: Internet
Author: User

Mainactivity as follows:
Package Cn.patience7;import Android.os.asynctask;import Android.os.bundle;import android.view.GestureDetector; Import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.view.viewtreeobserver;import Android.view.viewtreeobserver.onpredrawlistener;import Android.widget.relativelayout;import Android.app.activity;import android.content.context;/** * Demo Description: * Slide menu Slidingmenu Full Detail Example * * Layout file: * With relative layout, two interfaces overlap, respectively, Aboveview and Belowview. * * Implementation principle: * Listen to Aboveview touch events * i.e. Mbboveview.setontouchlistener (new Touchlistenerimpl ()) * in Touchlistenerimpl: * 1 A Ction_up will aboveview back to the left and right side of the screen * 2 actions other than action_up to hand gesture gesturedetector processing * * So through the aboveview move to cover or display Belowview, so as to achieve The effect of the side-by-side menu * * * NOTE: * The demo is similar to the previous two side-by-side menus, but is implemented using the Gesturedetector */public class Mainactivity extends Activity {priv Ate view Maboveview;private view mbelowview;private float scrollx = 0;private Context mcontext;private int screenwidth = 0 ;p Rivate Boolean Ismeasured = false;private int max_scroll_distance = 0;private gesturedetector mgesturedetector;private GestureListenerImpl Mgesturelistenerimpl;private slowlymoveasynctask mslowlymoveasynctask; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); InitView ();} private void Initview () {mcontext = This;mgesturelistenerimpl = new Gesturelistenerimpl (); mgesturedetector = new GestureD Etector (Mcontext, Mgesturelistenerimpl); mgesturedetector.setislongpressenabled (false); Maboveview = FindViewById ( R.id.abovelinearlayout); Maboveview.setontouchlistener (new Touchlistenerimpl ()); Mbelowview = FindViewById ( R.id.belowlinearlayout); InitData ();} /** * 1 Sets the width of the aboveview to the width of the screen, thus completely obscuring the Belowview * 2 max_scroll_distance for Aboveview to the left of the screen the maximum sliding distance */private void InitData () { Viewtreeobserver viewtreeobserver = Maboveview.getviewtreeobserver (); Viewtreeobserver.addonpredrawlistener (new Onpredrawlistener () {@Overridepublic Boolean onpredraw () {if (!ismeasureD) {screenwidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth (); Relativelayout.layoutparams aboveviewlayoutparams= (Relativelayout.layoutparams) mAboveView.getLayoutParams (); Aboveviewlayoutparams.width = Screenwidth;maboveview.setlayoutparams (aboveviewlayoutparams); Max_scroll_distance = Mbelowview.getwidth (); ismeasured = true;} return true;});} Private class Touchlistenerimpl implements Ontouchlistener {@Overridepublic Boolean onTouch (View V, motionevent event) {i F (event.getaction () = = motionevent.action_up) {relativelayout.layoutparams aboveviewlayoutparams= ( Relativelayout.layoutparams) Maboveview.getlayoutparams (); if (Aboveviewlayoutparams.leftmargin > (-screenWidth/ 2) {//When the finger slides to the left, it lifts up half the screen. Return mslowlymoveasynctask = new Slowlymoveasynctask (); mslowlymoveasynctask.execute;} else { When you slide your finger to the left, lift it over half the screen. Return mslowlymoveasynctask = new Slowlymoveasynctask (); Mslowlymoveasynctask.execute (-20);}} Return Mgesturedetector.ontouchevent (event);}} Private class Gesturelistenerimpl ImplementsGesturedetector.ongesturelistener {@Overridepublic Boolean ondown (Motionevent arg0) {return true;} @Overridepublic boolean onfling (motionevent arg0, motionevent arg1, float arg2,float arg3) {return false;} @Overridepublic void Onlongpress (Motionevent arg0) {} @Overridepublic boolean onscroll (motionevent arg0, motionevent Arg1,float Distancex, float distancey) {scrollx = Scrollx + Distancex; Relativelayout.layoutparams aboveviewlayoutparams= (Relativelayout.layoutparams) mAboveView.getLayoutParams (); Aboveviewlayoutparams.leftmargin = (int) (ABOVEVIEWLAYOUTPARAMS.LEFTMARGIN-SCROLLX);//The limit that the finger slides to the right to prevent crossing if ( Aboveviewlayoutparams.leftmargin >= 0) {aboveviewlayoutparams.leftmargin = 0;} The limit that the finger slides to the left to prevent crossing if (-aboveviewlayoutparams.leftmargin >= max_scroll_distance) { Aboveviewlayoutparams.leftmargin =-max_scroll_distance;} Maboveview.setlayoutparams (Aboveviewlayoutparams); return false;} @Overridepublic void Onshowpress (Motionevent motionevent) {} @Overridepublic boolean onsingletapup (MotioneveNT Motionevent) {return false;}} The following is an asynchronous task that handles the layout of the finger when it is lifted back to the left and right side of the screen to bounce the private class Slowlymoveasynctask extends Asynctask<integer, Integer, void> {@ overrideprotected Void doinbackground (Integer ... params) {Relativelayout.layoutparams aboveviewlayoutparams= ( Relativelayout.layoutparams) maboveview.getlayoutparams (); int leftMargin = aboveviewlayoutparams.leftmargin;// Number of times you need to move int move_times = 0;//total bounce distance int all_move_distance = 0;//each bounce distance int every_move_distance = Math.Abs (Params[0]); /move to the right of the screen if (Params[0] > 0) {all_move_distance = Math.Abs (leftMargin);//Go to the left of the screen} else {all_move_distance = Max_scroll_ Distance-math.abs (LeftMargin);}  Calculate the number of times to move if (all_move_distance% every_move_distance = = 0) {move_times = all_move_distance/every_move_distance;} else {move_times = all_move_distance/every_move_distance + 1;} System.out.println ("--all_move_distance=" + all_move_distance); System.out.println ("--every_move_distance=" + every_move_distance); System.out.println ("-Move_times= "+ move_times);//Move the process for (int i = 0; i < move_times; i++) {publishprogress (params[0]); try {thread.sleep ();} CA TCH (Exception e) {}}return null;} @Overrideprotected void Onprogressupdate (Integer ... values) {super.onprogressupdate (values); int every_move_distance = Values[0]; Relativelayout.layoutparams aboveviewlayoutparams= (Relativelayout.layoutparams) mAboveView.getLayoutParams (); if ( Every_move_distance > 0) {if (Aboveviewlayoutparams.leftmargin < 0) {Aboveviewlayoutparams.leftmargin + = Every_ move_distance;//handles possible cross-border situations after the last swipe if (Aboveviewlayoutparams.leftmargin > 0) {aboveviewlayoutparams.leftmargin = 0;} Maboveview.setlayoutparams (Aboveviewlayoutparams);}} else {if (Aboveviewlayoutparams.leftmargin > (-max_scroll_distance)) {aboveviewlayoutparams.leftmargin-= (-every_ move_distance);//handles possible cross-border situations after the last swipe if (Aboveviewlayoutparams.leftmargin <-max_scroll_distance) { Aboveviewlayoutparams.leftmargin =-max_scroll_distance;} Maboveview.setlayoutparams (aboveviewlayoutParams);}}}} 

Main.xml as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Line Arlayout android:id= "@+id/belowlinearlayout" android:layout_width= "Fill_parent" android:layout_height = "Fill_parent" android:layout_marginleft= "50dip" > <imageview android:layout_width= "Fill_pa Rent "android:layout_height=" fill_parent "android:scaletype=" Fitxy "android:src=" @drawabl e/a "/> </LinearLayout> <linearlayout android:id=" @+id/abovelinearlayout "Android:layout_w Idth= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "vertical" > <image View android:id= "@+id/imageview" android:layout_width= "Fill_parent" Android:layout_heigh t= "Fill_parent" android:scaletype="Fitxy" android:src= "@drawable/b"/> </LinearLayout></RelativeLayout> 


Full detailed example of the Android slide menu (hardcover version)

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.