Slidingmenu Open source Control side pull bar cannot slide problem fix, bug fix,

Source: Internet
Author: User

  Slidingmenu is a popular side-pull menu on GitHub Open Source control, a few days ago to write an open source control, after comparison, feel slidingmenu more powerful, but at the same time, their own writing of open source control, the sidebar can be sliding, such as this,
  Finger in the side of the sliding bar at the time, can still close the side pull bar, this function is very use, especially, when the slidingmenu relatively wide, occupy a relatively large proportion, at this time the user can only be left in a small range of sliding to close off, very pit dad has wood???? See most of the application, have this problem, so share it for everyone to learn together
  
  

But the problem came, to go through the source of the Slidingmenu, found that it does not provide such a method to set the side slider to slide,
What to do? Combined with the side bar control written in the first few days, you manually add this function to the source code,

Let's take a look at the side bar control you wrote. How to implement a sidebar slide

@Override Public Booleanonintercepttouchevent (motionevent ev) {//It is only possible to intercept when sliding sideways.        Switch(Ev.getaction ()) { CaseMotionEvent.ACTION_DOWN:downX= (int) Ev.getx ();  Break;  CaseMotionevent.action_move:intMoveX = (int) Ev.getx (); intdiff = Math.Abs (Downx-MoveX); if(diff >touchslop) {                return true; }             Break; default:             Break; }                return Super. onintercepttouchevent (EV); }

Code is not long, here is the use of Android inside the event distribution mechanism,
Override the Onintercepttouchevent method of a custom control to analyze user gesture actions,
When the user finger is sliding, and the x moving distance is greater than the Y moving distance, and is greater than touchslop (this is the default sliding distance of the system, when the moving distance is greater than this parameter, the default is 8, is the user finger sliding events),
Returns true, consumes this sliding event, and at this point, calls its own Ontouchevent method to pass the event to it.
  

@Override Public Booleanontouchevent (Motionevent event) {intScrollx; Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:downX= (int) Event.getx ();  Break;  CaseMotionevent.action_move:intMoveX = (int) Event.getx (); intDeltaX = Downx-MoveX; //determines whether the boundary can be exceeded after the given current incremental move.SCROLLX = GETSCROLLX () +DeltaX; if(Scrollx <-getchildat (0). Getmeasuredwidth ()) {//The left boundary is currently out of bounds and should be set to the left edge of the menu.ScrollTo (-getchildat (0). Getmeasuredwidth (), 0); } Else if(Scrollx > 0) {                //The right border is currently out of bounds and should be set to 0ScrollTo (0, 0); } Else{Scrollby (deltax,0); } downx=MoveX;  Break;  Casemotionevent.action_up://get half the width of the menu            intCenter =-getchildat (0). Getmeasuredwidth ()/2; SCROLLX= Getscrollx ();//the value in the upper-left corner of the current screen                        if(Scrollx >Center) {System.out.println ("Current switch to main interface"); Currentscreen=Screen_main; } Else{System.out.println ("Current Switch to menu interface"); Currentscreen=Screen_menu;            } switchscreen ();  Break; default:             Break; }        return true; }

Rewrite the Ontouchevent method, and then slide the screen according to the finger, the specific content is not detailed, the comments are in detail, need to specific source of the message can be discussed together!!

Next is the focus of today, how to let the Slidingmenu also achieve the effect of sliding side slider can be closed??
According to the code above, the principle is
    Analyze user gestures, and if they are sliding horizontally, intercept the event and then leave it to their own ontouchevent method to handle it.

Here, we need to slide the Slidingmenu control in the Ontouchevent method to
  

Let's take a look at the principle of the Slidingmenu control:

Slidingmenu mainly consists of two parts:

    1. The main interface is a customviewabove, when we use, we need to inherit the Slidingmenu class, and then Setcontentview (r.layout.content), in fact, this time to set the view to Customviewabove, move the entire Slidingmenu code in this class.
    2. The side pull bar interface is a customviewbehind, used when, similarly, when we go to Setbehindcontentview (r.layout.menu_frame); just set the view to its body, This class is representative of the side pull bar, need to implement the side pull bar function, need to do in this class inside the operation.

Get started:
Oneself here to go a lot of detours, in this will not in circles, direct to dry!!

  1. According to the principle that we have summed up before: we have to rewrite the Onintercepttouchevent method in the corresponding view of the sidebar, develop the Customviewbehind.java file discovery, it has been rewritten, so, in this
    We just need to add our own code, the original code only one line return!mchildrenenabled; We are here to add our own code between it!!!!

      
    @Override Public Booleanonintercepttouchevent (motionevent ev) {Switch(Ev.getaction ()) { CaseMotionEvent.ACTION_DOWN:downX= (int) Ev.getx ();  Break;  CaseMotionevent.action_move:intMoveX = (int) Ev.getx (); intdiff = Math.Abs (Downx-MoveX); if(diff > 8) {                return true; }             Break; }        return!mchildrenenabled; }

    The principle is the same as the first self-defined sidebar, judging gestures---> Intercept events

    1. Next, the event is sent to the Ontouchevent method, the same way that this code is provided, and there is only one line of return!mchildrenenabled;
      We need to slide the slidingmenu up here, how can we make it slide?
      I've been spending a lot of time here myself, because I can close the sidebar when I'm sliding in the main interface, which means the code must be in the main interface.
      Open Customviewabove.java, find the Ontouchevent method, see here
       CaseMotionevent.action_move:if(!misbeingdragged)                {Determinedrag (EV); if(Misunabletodrag)return false; }            if(misbeingdragged) {//Scroll to follow the motion event                Final intActivepointerindex =getpointerindex (EV, Mactivepointerid); if(Mactivepointerid = =invalid_pointer) Break; Final floatx =motioneventcompat.getx (EV, ACTIVEPOINTERINDEX); Final floatDeltaX = Mlastmotionx-x; Mlastmotionx=x; floatOLDSCROLLX =Getscrollx (); floatSCROLLX = Oldscrollx +DeltaX; Final floatLeftbound =Getleftbound (); Final floatRightbound =Getrightbound (); if(Scrollx <leftbound) {SCROLLX=Leftbound; } Else if(Scrollx >rightbound) {SCROLLX=Rightbound; }                //Don ' t lose the rounded componentMlastmotionx + = Scrollx-(int) Scrollx; ScrollTo ((int) Scrollx, getscrolly ()); Pagescrolled ((int) SCROLLX); }             Break;

      Other don't look, look at its line of notes,

      // Scroll to follow the motion event

      The translation comes to the beginning of the meaning of sliding, indicating that the next code is a sliding code, but there is a problem, it has a judgment condition,

      if (! misbeingdragged) {                Determinedrag (EV);                 if (Misunabletodrag)                     return false ;            }             if (misbeingdragged) {
          

      We must find a way to set misbeingdragged to true, fearing that the code will fail to execute.

    2. Understand these reasons, now is good to do, back to the Customviewbehind.java class, this time, our demand is to call Customviewabove.java this class Ontouch method, need this class of object, where to find the object?
      Fortunately, there is no place to find pale, have to pay no effort, inadvertently see the Customviewbehind class has a member variable
      Private Customviewabove Mviewabove;

      The original has been provided, the harm I find!!!

    3. Next the last code is done, two steps
      1. set the misbeingdragged to true first
      2. call the Ontouchevent method of the Customviewabove class again
      @Override      Public Boolean ontouchevent (motionevent e) {        // try to get Slidingmenu to slide        mviewabove.misbeingdragged =true;        Mviewabove.ontouchevent (e);         return ! mchildrenenabled;    }
      Test, done.

  2. Final summary
    1. Code in fact very few, just a few lines, but it took a lot of time, mainly code nesting too much, difficult to find the focus
    2. summed up, the implementation of the three-step, that is, analysis of user gestures---> Intercept events--handling events, call the relevant code to implement sliding
    3. The event distribution mechanism is the focus, and the next step is to study!!!!

  

   

Slidingmenu Open source Control side pull bar cannot slide problem fix, bug fix,

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.