Android Development--Slide sidebar implementation

Source: Internet
Author: User

In Android app development, sliding sidebar is often used, today I also try to do a simple practice, although the function is not very powerful, but can be preserved for future development use, there is a need to make simple changes. The idea of implementing a sliding sidebar is also simple:

1. Rewrite a slidingmenu class inheritance viewgroup, critically ill the ViewGroup add two sub-layouts, respectively, for the menu and the main interface display;

2. In order to get a sliding effect, choose Scroller to help us achieve, with viewgroup under the Computescroll method to achieve the interface update;

3. Use a Boolean to record whether the menu is open, swipe to the right when the menu is open, and slide left without responding when the menu is closed;

4. In order to get a good interaction, we can define a scale for the distance between the interface slide and the finger movement, such as each touch event occurs, the interface moves only half the distance of the finger movement;

Here are two, the interface is not how to layout, we do not look

Slidingmenu Implementation code:

Package Com.example.test;import Android.content.context;import Android.view.motionevent;import Android.view.View; Import Android.view.viewgroup;import Android.widget.scroller;public class Slidingmenu extends ViewGroup {private Static final String TAG = SlidingMenu.class.getName ();p rivate enum Scroll_state {scroll_to_open, scroll_to_close;} Private scroll_state state;private int mmostrecentx;private int downx;private Boolean isOpen = False;private View menu;pri Vate View mainview;private Scroller mscroller;private onslidingmenulistener onslidingmenulistener;public SlidingMenu ( Context context, view main, View menu) {super (context);//TODO Auto-generated constructor Stubsetmainview (main); SetMenu ( menu); init (context);} private void init (context context) {Mscroller = new Scroller (context);} @Overrideprotected void OnLayout (boolean arg0, int l, int t, int r, int b) {//TODO auto-generated method Stubmainview.lay Out (L, T, R, b); Menu.layout (-menu.getmeasuredwidth (), T, 0, B); public void Setmainview(View view) {MainView = View;addview (MainView);} public void SetMenu (view view) {menu = View;addview (menu);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated method Stubsuper.onmeasure (Widthmeasurespec, Heightmeasurespec); Mainview.measure (Widthmeasurespec, HeightMeasureSpec); Menu.measure (widthMeasureSpec-150, heightmeasurespec);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: Mmostrecentx = (int) event.getx ();d ownx = (int) event.getx (); break;case MotionEvent.ACTION_MOVE:int MoveX = (int) event.g EtX (); int deltax = mmostrecentx-movex;//If you swipe to the right while the menu is open and swipe left when the menu closes, the scroll event is not triggered if (!isopen && (Downx-movex) < 0) | | (IsOpen && (Downx-movex) > 0)) {Scrollby (DELTAX/2, 0);} Mmostrecentx = movex;break;case MotionEvent.ACTION_UP:int UpX = (int) event.getx (); int dx = upx-downx;if (!isopen) {//Dish Single off//swipe right over menu half width to open menu if (dx > Menu.getmeasuredwidth ()/3) {state = Scroll_state.scroll_to_open,} else {state = Scroll_state.scroll_to_close;}} else {/////When the touch point is pressed while in the menu area, it will only be closed when the touch point in the menu region is down////When the tap is pressed in the main area, the IF (Downx < Menu.getmeasuredwidth () {if (dx <-menu.getmeasuredwidth ()/3) {state = Scroll_state.scroll_to_close;} else {state = Scroll_state.scroll_to_ Open;}} else {state = Scroll_state.scroll_to_close;}} Smoothscrollto (); break;default:break;} return true;} private void Smoothscrollto () {int scrollx = GETSCROLLX (); switch (state) {Case Scroll_to_Close:mScroller.startScroll ( SCROLLX, 0,-SCROLLX, 0, $); if (Onslidingmenulistener! = null && isOpen) {onslidingmenulistener.close ();} IsOpen = False;break;case Scroll_to_Open:mScroller.startScroll (scrollx, 0,-scrollx-menu.getmeasuredwidth (), 0, 500); if (Onslidingmenulistener! = null &&!isopen) {onslidingmenulistener.close ();} IsOpen = True;break;default:break;}} @Overridepublic void Computescroll () {if (Mscroller.computescrolloffset ()) {ScrollTo (MscrolleR.getcurrx (), 0);} Invalidate ();} public void Open () {state = Scroll_state.scroll_to_open;smoothscrollto ();} public void Close () {state = Scroll_state.scroll_to_close;smoothscrollto ();} public Boolean IsOpen () {return isOpen;} public void Setonslidingmenulistener (Onslidingmenulistener onslidingmenulistener) {This.onslidingmenulistener = Onslidingmenulistener;} Public interface Onslidingmenulistener {public void open ();p ublic void Close ();}}
Making calls in Mainactivity
Package Com.example.test;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity {private button openbutton;private button CloseButton; Private Slidingmenu mslidingmenu; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( savedinstancestate); mslidingmenu = new Slidingmenu (this, Layoutinflater.from (this). Inflate (R.layout.fragment1, NULL ), Layoutinflater.from (this). Inflate (R.layout.fragment2, null)); Setcontentview (mslidingmenu);// Note that Setcontentview needs to be replaced by our Slidingmenuopenbutton = (button) Findviewbyid (r.id.button1); CloseButton = (Button) Findviewbyid (R.id.button_close); Openbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick ( View arg0) {//TODO auto-generated method Stubmslidingmenu.open ();}}); Closebutton.setonclicklistener (New Onclicklistener () {@OverridepublIC void OnClick (View arg0) {//TODO auto-generated method Stubmslidingmenu.close ();}});}} 



Android Development--Slide sidebar implementation

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.