Complete and detailed examples of the Android slide menu (hardcoded Version)
MainActivity is 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: * complete and detailed example of sliding menu SlidingMenu ** layout file: * uses a relative layout. The two interfaces overlap, namely, aboveView and belowView. ** implementation principle: * Listen to the Touch event of the aboveView * that is, mBboveView. setOnTouchListener (new TouchListenerImpl () * in TouchListenerImpl: * 1 when ACTION_UP, return the aboveView to the left and right sides of the screen. * 2 Action except ACTION_UP is handed over to GestureDetector for processing. ** therefore, the abveview is moved to hide or display the belowView, to achieve the effect of the slide menu ** Note: * this Demo is similar to the previous two slide menus, but the GestureDetector */pu is used in code implementation. Blic class MainActivity extends Activity {private View mAboveView; private View mBelowView; private float scrollX = 0; private Context mContext; private int screenWidth = 0; private boolean isMeasured = false; private int MAX_SCROLL_DISTANCE = 0; private GestureDetector mGestureDetector; private GestureListenerImpl mGestureListenerImpl; private SlowlyMoveAsyncTask mSlowlyMoveAsyncTask; @ Overrideprotected vo Id onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initView ();} private void initView () {mContext = this; Attributes = new GestureListenerImpl (); mGestureDetector = new GestureDetector (mContext, role); mGestureDetector. setIsLongpressEnabled (false); mAboveView = findViewById (R. id. aboveLinearLayout); mAboveView. setOnTouchListe Ner (new TouchListenerImpl (); mBelowView = findViewById (R. id. belowLinearLayout); initData () ;}/ *** 1 set the width of the aboveView to the width of the screen, so that belowView * 2 MAX_SCROLL_DISTANCE is completely concealed as the maximum sliding distance from aboveView to the left of the screen */private void initData () {ViewTreeObserver viewTreeObserver = mAboveView. getViewTreeObserver (); viewTreeObserver. addOnPreDrawListener (new OnPreDrawListener () {@ Overridepublic boolean onPreDraw () {if (! IsMeasured) {screenWidth = getWindowManager (). getdefadisplay display (). 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) {if (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 is lifted when it is not halfway through the screen. return mSlowlyMoveAsyncTask = new slowlymoveasynctask(~~mslowlymoveasynctask.exe cute (20);} else {// when your finger slides to the left, it is lifted when half of the screen is reached. return mSlowlyMoveAsyncTask = new slowlymoveasynctask(~~mslowlymoveasynctask.exe cute (-20);} return mGestureDetector. onTouchEvent (event) ;}} private class GestureListenerImpl implements GestureDetector. 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 + rollback; enabled. layoutParams aboveViewLayoutParams = (RelativeLayout. layoutParams) mAboveView. getLayoutParams (); aboveViewLayoutParams. leftMargin = (int) (aboveViewLayoutParams. leftMargin-scrollX); // the limit that the finger slides to the right to prevent cross-border if (abveviewlayoutparams. leftMargin> = 0) {aboveViewLayoutParams. leftMargin = 0;} // the limit that the finger slides to the left to prevent cross-border if (-aboveViewLayoutParams. leftMargin> = MAX_SCROLL_DISTANCE) {aboveViewLayoutParams. leftMargin =-MAX_SCROLL_DISTANCE;} mAboveView. setLayoutParams (callback); return false ;}@ Overridepublic void onShowPress (MotionEvent motionEvent) {}@ Overridepublic boolean onSingleTapUp (MotionEvent motionEvent) {return false ;}// the following is an asynchronous task, responsible for handling the private class SlowlyMoveAsyncTask extends AsyncTask to the left and right sides of the screen when the finger is raised
{@ Overrideprotected Void doInBackground (Integer... params) {RelativeLayout. layoutParams aboveViewLayoutParams = (RelativeLayout. layoutParams) mAboveView. getLayoutParams (); int leftMargin = aboveViewLayoutParams. leftMargin; // number of times to be moved int move_times = 0; // The total number of times to be moved back is int all_move_distance = 0; // The distance from int every_move_distance = Math. abs (params [0]); // move it to the right of the screen if (params [0]> 0) {all_move_distance = Math. abs (leftMargin); // move 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); // The moving process for (int I = 0; I <move_times; I ++) {publishProgress (params [0]); try {Thread. sleep (20);} catch (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; // if (aboveViewLayoutParams. leftMargin> 0) {aboveViewLayoutParams. leftMargin = 0;} mAboveView. setLayoutParams (aboveViewLayoutParams) ;}} else {if (aboveViewLayoutParams. leftMargin> (-MAX_SCROLL_DISTANCE) {aboveViewLayoutParams. leftMargin-= (-every_move_distance); // if (aboveViewLayoutParams. leftMargin <-MAX_SCROLL_DISTANCE) {aboveViewLayoutParams. leftMargin =-MAX_SCROLL_DISTANCE;} mAboveView. setLayoutParams (aboveViewLayoutParams );}}}}}
Main. xml is as follows: