Android Slide Sidebar slidemenu source code Analysis and User manual

Source: Internet
Author: User
Tags gety xdiff

Android Slide Sidebar slidemenu source code Analysis and User manual




Let's look at the usage scenario first:




First of all
public class Mainactivity extends slidingfragmentactivity
Inherit Slidingfragmentactivity, which is created with the necessary slidemenu and other related data.


Add inside the OnCreate
private void Init_sm () {
SM = Getslidingmenu (); Get Menu
Sm.setmode (slidingmenu.left_right);//set to both left and right
Sm.settouchmodeabove (Slidingmenu.touchmode_fullscreen);//set to right full screen mode
Sm.settouchmodebehind (Slidingmenu.touchmode_margin);//set to 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
}
Here we want to detail the Touchmode_fullscreen this parameter, set to this parameter, the right side of the layout will not be able to respond to the sliding message, because the full-screen message is slidemenu processed out.
So we need to revise the following:
Sm.addignoredview (Mpager);
Use this method to ignore messages that click to Mpager view.
After setting ignore, you need to use Setontouchlistener to receive the response touchscreen message 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 > 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. On the left is a menu, simple layout, the right is a viewpage, so swipe to the far left, and then slide out of the ViewPage to enter the main interface.
ShowMenu and Showsecondarymenu to manipulate the menu bar showing the left and right sides
Use on these points of attention can be.


The mainactivity XML file 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 joins the container layout as:


Fragmenttransaction t = This.getsupportfragmentmanager ()
. BeginTransaction ();
Centerfragment = new Samplelistfragment ();
Centerfragment.setcontext (Mainactivity.this);
T.add (R.id.container, centerfragment);
T.commit ();

-------------------------
Below to analyze the source code:
First of all, because we inherit the public class Mainactivity extends slidingfragmentactivity so we first study this slidingfragmentactivity


View OnCreate functions
Mhelper = new Slidingactivityhelper (this);
Mhelper.oncreate (savedinstancestate);
This is a key attribute, and many of the subsequent interfaces are handled through it. Like what
Public Slidingmenu Getslidingmenu () {
return Mhelper.getslidingmenu ();
}
So we're going to see the Slidingactivityhelper class.
See Mhelper.oncreate (savedinstancestate); this function
public void OnCreate (Bundle savedinstancestate) {
Mslidingmenu = (slidingmenu) layoutinflater.from (mactivity). Inflate (r.layout.slidingmenumain, NULL);
}
Created a key Property object
R.layout.slidingmenumain content is:
<?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 the Slidingmenu appeared.


Key second function:
Onpostcreate
Inside the method:


Mslidingmenu.attachtoactivity (Mactivity,
Menableslide? SlidingMenu.SLIDING_WINDOW:SlidingMenu.SLIDING_CONTENT);


Hang yourself up to the activity's root view
Look at this paragraph:
Case Sliding_content:
Mactionbaroverlay = Actionbaroverlay;
Take the above view out of
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
Here the processing logic is:
Contentparent.addview (this);
Add Slidemenu to this contentview, use setcontent (content) as the context of the current activity, and set it in Contentview.
In this case, the Slidemenu is added to the main interface.


Then the message will be passed to Customviewabove.java and Customviewbehind.java two are inherited ViewGroup, so you can add more than a few view.


Because Slideingmenu adds mviewabove to the back, it has priority to respond to message rights. Then, based on the setcontent (content), set the Slidemenu layout in, so that
The top message will be passed to it.
In Customviewabove Onintercepttouchevent and Ontouchevent will deal with all kinds of messages, so collocation, then finally realize the function of the left and right side sidebar.




The container touch screen process for the main interface is: No response will be passed to the touch up dispatch to Com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove


Android Slide Sidebar slidemenu source code Analysis and User manual

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.