Complete and detailed examples of Android slide menus (hardcoded version) and android hardcoded version

Source: Internet
Author: User

Complete and detailed examples of Android slide menus (hardcoded version) and android 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 <Integer, Integer, Void >{@ 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:
<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" >    <LinearLayout        android:id="@+id/belowLinearLayout"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_marginLeft="50dip" >        <ImageView            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:scaleType="fitXY"            android:src="@drawable/a" />    </LinearLayout>    <LinearLayout        android:id="@+id/aboveLinearLayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <ImageView             android:id="@+id/imageView"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:scaleType="fitXY"            android:src="@drawable/b" />    </LinearLayout></RelativeLayout>



Now, is it better for android to use the SlidingMenu open-source project to slide the menu bar, or is it better to use the Navigation Drawer that comes with android?

If you are learning the code, it is recommended to study the official version. However, SlidingMenu has many functions, such as controlling the background behind the menu when sliding out. It seems that the background of Navigation Drawer cannot be moved. It is a trend to replace open-source projects with official APIs, because everyone thinks that official interfaces are safe to use, but open-source projects may be more flexible.

How can I solve the conflict between the image switching and the displayed slide menu on Android?

For details, what is the situation.

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.