Current location - Recipe Complete Network - Catering industry - How does Android slidemenu slide the left menu box to monitor the click events of menu items?
How does Android slidemenu slide the left menu box to monitor the click events of menu items?
Package com.qm.test;

Import com.jeremyfeinstein.slidingmenu.lib.slidingmenu;

Import com.jeremyfeinstein.slidingmenu.lib.slidingmenu.canvas transformer;

Import com.jeremyfeinstein.slidingmenu.lib.app.slidingfragment activity;

Import android.graphics.canvas;

Import android.os.bundle;

Import android.support.v4.app.fragment;

Import android.support.v4.app.fragmenttransaction;

Import android.view.keyevent;

Import android.view.menuitem;

Import android.view.view;

Import Android.view.view.onclicklistener;

Import android.view.animation.interpolator;

Import android.widget.textview;

Import android.widget.toast;

/*

* main page packaging menu, return to animation, exit the event packaging class.

*/

Public class ListActivityMode extends SlidingFragmentActivity {

Private CanvasTransformer mTransformer

Private sliding menu sm;

public void on create(Bundle saved instancestate){

super . oncreate(savedInstanceState);

initSlidingMenu(this);

getActionBar()。 setDisplayHomeAsUpEnabled(true);

}

/*

* When the home page is not visible, close the menu.

* @ See android.support.v4.app.fragmentactivity # onstop ()

*/

@ Overlay

Protected void onStop() {

// TODO automatically generated method stub

if(sm.isMenuShowing()){

sm . toggle();

}

super . onstop();

}

Private long exit time = 0;;

/*

* Double-click the back button to exit.

* @ see Android . support . v4 . app . fragment activity # onKeyDown(int,android.view.KeyEvent)

*/

@ Overlay

public boolean onKeyDown(int key code,KeyEvent event) {

If (KeyEvent. KEYCODE_BACK == keyCode) {

//Judge whether the Enter key is clicked continuously within two seconds. If yes, exit; otherwise, do not exit.

if(system . current time millis()-exit time & gt; 2000) {

Toast. Maketext (getapplicationcontext ()), "Press again to exit the program",

Toast. LENGTH_SHORT)。 show();

//Assign the current system time to exitTime.

exit time = system . current time millis();

} Otherwise {

finish();

system . exit(0);

}

Return true

}

Returns super.onKeyDown(keyCode, event);

}

/*

* menu initialization program

* @ see Android . app . activity # onoptionsitems elected(Android . view . menuitem)

*/

@ Overlay

Public boolean on options item selected {

switch (item.getItemId()) {

case android。 Id home page:

Switch ();

Return true

}

Returns super. onoptionsitems selected (item);

}

/**

* Initialize the sliding menu

*/

Public void init sliding menu (sliding fragmentation activity) {

//Initialize the animation effect

InitAnimation (activity);

//Set the main interface view

//activity . setcontentview(r . layout . activity _ main);

//Set the sliding menu view

activity . setbehindcontentview(r . layout . menu _ frame);

activity . getsupportfragmentmanager()。 beginTransaction()。 replace(R.id.menu_frame,new SampleListFragment())。 commit();

//Set the property value of the sliding menu.

sm = activity . getslidingmenu();

sm . setshadowwidthres(r . dimen . shadow _ width);

sm . setshadowdrawable(r . drawable . shadow);

sm . setbehindoffsetres(r . dimen . sliding menu _ offset);

sm . setfadedegree(0.35 f);

sm . settouchmodeabove(sliding menu。 TOUCHMODE _ full screen);

sm . setbehindscrollscale(0.0f);

sm . setbehindcanvastransformer(mTransformer);

activity . setslidingactionbarenabled(true);

}

private Interpolator interp = new interpolar(){

@ Overlay

Public float get interpolation (float t) (

t-= 1.0f;

Returns t * t * t+1.0f;

}

};

/**

* Initialize animation effect

*/

Private Void Init Animation (SlidingFragmentActivity) (

mTransformer = new CanvasTransformer(){

@ Overlay

public void transform Canvas(Canvas Canvas,float percentOpen) {

canvas.translate(0,canvas . get height()*( 1-interp . get interpolation(percent open)));

}

};

}

}