Android SlideMenu source code analysis and user manual, androidslidemenu

Source: Internet
Author: User
Tags gety xdiff

Android SlideMenu source code analysis and user manual, androidslidemenu
Android SlideMenu source code analysis and User Manual




Let's first look at the usage:




First
Public class MainActivity extends SlidingFragmentActivity
Inherit from SlidingFragmentActivity, where slidemenu and other required data are created.


Add in Oncreate
Private void init_sm (){
Sm = getSlidingMenu (); // obtain the menu
Sm. setMode (SlidingMenu. LEFT_RIGHT); // It is set to either left or right.
Sm. setTouchModeAbove (sjavasingmenu. TOUCHMODE_FULLSCREEN); // set it to full screen on the right.
Sm. setTouchModeBehind (SlidingMenu. TOUCHMODE_MARGIN); // set it to the left border mode.
SetBehindContentView (R. layout. slidingmenu_left); // left layout
Sm. setSecondaryMenu (R. layout. slidingmeun_right); // right layout
Sm. setRightBehindWidth (MyApplication) getApplication ())
. GetScreenWidth (); // use this to layout the right side to full screen
}
The parameter TOUCHMODE_FULLSCREEN is described in detail here. After this parameter is set, the layout on the right side cannot respond to the sliding message, because full-screen messages are processed by slidemenu.
Therefore, we need to modify it as follows:
Sm. addIgnoredView (mPager );
Use this method to ignore the message clicked to mPager view.
After ignore is set, you need to use setOnTouchListener to receive the touch screen response message by yourself.


MPager. setOnTouchListener (new OnTouchListener (){
Private float startx =-1, starty =-1, OffsetWidth;
Private boolean hasdown = false;


@ Override
Public boolean onTouch (View v, MotionEvent event ){
Int action = event. getAction ();
Boolean returns = false;
If (action = MotionEvent. ACTION_DOWN ){
Startx = event. getX ();
Starty = event. getY ();
Hasdown = true;
} Else if (action = MotionEvent. ACTION_UP
| Action = MotionEvent. ACTION_CANCEL
| Action = MotionEvent. ACTION_MASK ){
Float x = event. getX ();
Float y = event. getY ();
Float xdiff = Math. abs (x-startx );
Float ydiff = Math. abs (y-starty );
If (! (-1 = startx &-1 = starty )){
If (OffsetWidth <1)
OffsetWidth = (float) (MyApplication
. GetApplication (). GetScreenWidth () * 0.15 );
If (hasdown & xdiff & gt; ydiff * 2.2
& Xdiff> OffsetWidth ){
If (x> startx )//
{
If (currIndex = 0 ){
Sm. toggle ();
}
Returns = false;
} // Else if (startx> x )//
//{
// If (currIndex = length-1
//&&! Sm. isSecondaryMenuShowing ())
// Sm. showSecondaryMenu ();
//}
}
}
Hasdown = false;
Startx = starty =-1;
}
Return returns;
}
});



The current practice is
If (currIndex = 0 ){
Sm. toggle ();
}
Use this to exit the sidebar. There is a menu on the left, simple layout, and viewpage on the right. Therefore, when sliding to the far left side, the page will be moved out to enter the main interface.
ShowMenu and showSecondaryMenu
Pay attention to these points in use.


The xml file of mainactivity is:


<FrameLayout 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"
Tools: context = "com. example. atemp. MainActivity"
Tools: ignore = "MergeRootFrame">


<FrameLayout
Android: id = "@ + id/container"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
</FrameLayout>


</FrameLayout>


The Code adds the iner to the layout:


FragmentTransaction t = this. getSupportFragmentManager ()
. BeginTransaction ();
CenterFragment = new SampleListFragment ();
CenterFragment. setcontext (MainActivity. this );
T. add (R. id. container, centerFragment );
T. commit ();

-------------------------
Next we will analyze the source code:
First, because we inherit the public class MainActivity extends SlidingFragmentActivity, we should first study this SlidingFragmentActivity


View the oncreate Function
MHelper = new SlidingActivityHelper (this );
MHelper. onCreate (savedInstanceState );
This is a key attribute, and many subsequent interfaces are processed through it. For example
Public SlidingMenu getSlidingMenu (){
Return mHelper. getSlidingMenu ();
}
So let's take a look at the SlidingActivityHelper class.
Check mHelper. onCreate (savedInstanceState); this function
Public void onCreate (Bundle savedInstanceState ){
MSlidingMenu = (SlidingMenu) LayoutInflater. from (mActivity). inflate (R. layout. slidingmenumain, null );
}
Key property object created
R. layout. slidingmenumain content:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Com. jeremyfeinstein. slidingmenu. lib. SlidingMenu xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/slidingmenumain"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
So slidingMenu appears.


Key second function:
OnPostCreate
Method:


MSlidingMenu. attachToActivity (mActivity,
MEnableSlide? SlidingMenu. SLIDING_WINDOW: SlidingMenu. SLIDING_CONTENT );


Mount yourself to the Root view of the activity
Take a look at this section:
Case SLIDING_CONTENT:
MActionbarOverlay = actionbarOverlay;
// Take the above view out
ViewGroup contentParent = (ViewGroup) activity. findViewById (android. R. id. content );
View content = contentParent. getChildAt (0 );
ContentParent. removeView (content );
ContentParent. addView (this );
SetContent (content );
// Save people from having transparent backgrounds
If (content. getBackground () = null)
Content. setBackgroundResource (background );
Break;
The processing logic here is:
ContentParent. addView (this );
Add slidemenu to the contentview and use setContent (content) as the context of the current activity.
In this way, the slidemenu will be added to the main interface.


Then the message will be sent to CustomViewAbove. java and CustomViewBehind. java both inherit the viewgroup, so you can add several more views.


Because slideingMenu adds mViewAbove to the backend, it has the right to give priority to message response. Then, according to setContent (content);, set the layout with slidemenu,
The top message is sent to it.
The onInterceptTouchEvent and onTouchEvent of CustomViewAbove process various types of messages. In this way, the left and right sidebar functions are implemented.




The container Touch screen process on the main interface is: it will be passed to Touch up dispatch to com. jeremyfeinstein. slidingmenu. lib. CustomViewAbove only when no response is returned.


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.