Android left and Right slide example

Source: Internet
Author: User

Android left and right slide example. Can self-set the effect of skidding, I am here to achieve is around can. The side-by-side interface can initiate activity or load view. The slide-slip effect is handled using fragmentation rules. Detailed comments in the code, look at the implementation, and then look at the project structure and resource file distribution. Code only paste part, provide the source code download link.

SOURCE Download: click

1. Realize


2. Project Structure Chart


3. Resource distribution Map


4, Leftfragment

Package Com.org.fragment;import Com.org.activity.r;import Com.org.activity.testactivity;import Android.content.intent;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.view.view.onclicklistener;public class Leftfragment extends Fragment implements Onclicklistener{private Layoutinflater minflater; @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {View view = Inflater.inflate (R.layout.main_left_fragment, Container,false); InitView (view); return view;} @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);} @Overridepublic void Onresume () {Super.onresume ();} private void Initview (view view) {//To obtain a context context handle from getactivity to load other required View layouts Minflater = Layoutinflater.from ( Getactivity ()); View.findviewbyid (R.id.button1). Setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.button1://starts in fragment processing activityintent tochatintent = new Intent (getactivity (), testactivity.class); StartActivity (tochatintent); break;default:break;}}

5, Rightfragment

Package Com.org.fragment;import Com.org.activity.r;import Com.org.slidinglayer.slidinglayer;import Com.org.slidinglayer.slidinglayer.oninteractlistener;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.textview;public Class Rightfragment extends Fragment implements Onclicklistener, Oninteractlistener{private Layoutinflater minflater; Private Slidinglayer mslidinglayer;private View mtestview; @Overridepublic void OnCreate (Bundle savedinstancestate) {// TODO auto-generated Method Stubsuper.oncreate (savedinstancestate);} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {View view = Inflater.inflate (R.layout.main_right_fragment, Container,false); Initview (view); return view;} private void Initview (view view) {View.findviewbyid (r.id.button1). Setonclicklistener (this);//rightfraGment the sliding implementation of the page inside the Mslidinglayer = (slidinglayer) View.findviewbyid (R.id.right_sliding_layer); Mslidinglayer.setoninteractlistener (this);//To get the context handle from getactivity Minflater = Layoutinflater.from ( Getactivity ());//Display Test View page, load view page via Layoutinflater Mtestview = minflater.inflate (r.layout.activity_test, NULL) Mtestview.findviewbyidtextview Testtextview = (TextView) Mtestview.findviewbyid (R.ID.TEXTVIEW1) is the one whose view is used; Testtextview.settext ("Test display View"); @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.button1:mslidinglayer.removeallviews ();// Remove all the view first, otherwise it will error if (!mslidinglayer.isopened ()) {Mslidinglayer.addview (Mtestview); Mslidinglayer.openlayer (true);} Break;default:break;}} @Overridepublic void OnOpen () {//Todo auto-generated method stub} @Overridepublic void OnClose () {//Todo auto-generated m Ethod stub} @Overridepublic void Onopened () {//Todo auto-generated method stub} @Overridepublic void Onclosed () {//Todo Au To-generated method Stubmslidinglayer.removeallviews ();}}

6, Mainactivity

Package Com.org.activity;import Com.org.fragment.leftfragment;import Com.org.fragment.rightfragment;import Com.org.slidingmenu.baseslidingfragmentactivity;import Com.org.slidingmenu.slidingmenu;import Android.content.context;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmenttransaction;import Android.util.displaymetrics;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.button;import Android.widget.toast;public class Mainactivity extends Baseslidingfragmentactivity implements onclicklistener{ protected Slidingmenu mslidingmenu; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); requestwindowfeature (Window.feature_no_title); Initslidingmenu (); SetContentView ( R.layout.activity_main); Initview (savedinstancestate);} private void Initslidingmenu () {//This is set in, the default layout is loaded by key, and then replaced with fragments, only in this way can leftfragment control Setbehindcontentview ( R.layout.main_left_layout);//Set left menu//This is a fragment processing rule, replace the main_left_fragment layout, with the left slide implementation of this layout//without this, the load layout is only the default Main_left_ Fragmentfragmenttransaction mfragementtransaction = Getsupportfragmentmanager (). BeginTransaction (); Fragment Mfrag = new Leftfragment (); Mfragementtransaction.replace (R.id.main_left_fragment, Mfrag); Mfragementtransaction.commit ();//Customize the Slidingmenudisplaymetrics DM = new Displaymetrics (); Getwindowmanager ( ). Getdefaultdisplay (). Getmetrics (DM); int mscreenwidth = dm.widthpixels;//Get screen resolution width Mslidingmenu = Getslidingmenu (); Mslidingmenu.setmode (slidingmenu.left_right);//set is left or right slide, or around can be slippery, I can slip around here mslidingmenu.setshadowwidth ( MSCREENWIDTH/40);//Set Shadow width Mslidingmenu.setbehindoffset (MSCREENWIDTH/8);//Set menu width mslidingmenu.setfadedegree (0.35f );//Set the fade ratio mslidingmenu.settouchmodeabove (slidingmenu.touchmode_margin); Mslidingmenu.setshadowdrawable ( R.drawable.slidingmenu_shadow)///Set left menu Shadow Picture mslidingmenu.setsecondaryshadowdrawable (R.drawable.right_shadow);// Set right menu Shadow picture mslidingmenu.setfadeenabled (true);/sets whether the menu fades in and out Mslidingmenu.setbehindscrollscale (0.333f) when sliding, and//sets the Drag effect}private void Initview (Bundle Savedinstancestate) {//This is set in, the default layout is loaded by key, and then replaced with fragments Mslidingmenu.setsecondarymenu (r.layout.main_right_layout) ;//This is a fragment processing rule, replacing the main_right_fragment layout, by right-sliding implementation of this layout//without this, the load layout is just the default main_right_fragmentfragmenttransaction Mfragementtransaction = Getsupportfragmentmanager (). BeginTransaction (); Fragment Mfrag = new Rightfragment (); Mfragementtransaction.replace (R.id.main_right_fragment, Mfrag); Mfragementtransaction.commit ();((button) Findviewbyid (R.id.buttonleft)). Setonclicklistener (this);((button) Findviewbyid (R.id.buttonright)). Setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.buttonleft:mslidingmenu.showmenu (true); break;case R.id.buttonright:mslidingmenu.showsecondarymenu (true); break;default:break;}} /** * Press two times to return the key to exit */private long firsttime; @Overridepublic void onbackpressed () {//TODO auto-generated method Stubif (Sys Tem.currenttimemillis ()-FirsttiMe < +) {finish ();} else {firsttime = System.currenttimemillis (); Showshort (This, "Press again to exit!) ");}} static toast toast;public static void Showshort (context context, String message) {if (null = = toast) {toast = Toast.makete XT (context, message, toast.length_short);//toast.setgravity (gravity.center, 0, 0);} else {toast.settext (message);} Toast.show ();}}

SOURCE Download: click


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android left and Right slide example

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.