Sliding Collision of Android slide menu and Carousel diagram

Source: Internet
Author: User

To take over a project, there is a problem that needs to be modified: The carousel diagram cannot be manually slid, and manual sliding of the carousel will only trigger a slide-off menu.

Guess: The touch events of the Viewpager Control (Carousel map) were intercepted by the Slidingmenu control (slide-off menu, non-third-party project, which was the last developer-defined).

Based on this speculation, I customize a viewpager, rewrite the dispatchtouchevent, onintercepttouchevent, and Ontouchevent, respectively, in these three methods to print a log;

Rewrite Slidingmenu's dispatchtouchevent, Onintercepttouchevent, and ontouchevent, and print log as well.

Recompile run, hand pulley plot, log as follows:

06-08 09:52:08.394 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:006-08 09:52:08.395 19424-19424/ Com.parkingmore e/slidingmenu:onintercepttouchevent ev:006-08 09:52:08.395 19424-19424/com.parkingmore E/ Rollviewpager:dispatchtouchevent ev:006-08 09:52:08.395 19424-19424/com.parkingmore E/RollViewPager: Onintercepttouchevent ev:006-08 09:52:08.441 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:206-08 09:52:08.441 19424-19424/com.parkingmore e/slidingmenu:onintercepttouchevent ev:206-08 09:52:08.442 19424-19424/ Com.parkingmore e/slidingmenu:action_move dx:15.47399906-08 09:52:08.442 19424-19424/com.parkingmore E/ Rollviewpager:action_move Getcurrentitem (): 106-08 09:52:08.442 19424-19424/com.parkingmore E/RollViewPager: Dispatchtouchevent ev:206-08 09:52:08.442 19424-19424/com.parkingmore e/rollviewpager:onintercepttouchevent Ev : 206-08 09:52:08.459 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:206-08 09:52:08.459 19424-19424 /com.Parkingmore e/slidingmenu:onintercepttouchevent ev:206-08 09:52:08.459 19424-19424/com.parkingmore E/RollViewPager: Dispatchtouchevent ev:306-08 09:52:08.459 19424-19424/com.parkingmore e/rollviewpager:onintercepttouchevent Ev : 306-08 09:52:08.477 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:206-08 09:52:08.477 19424-19424 /com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.495 19424-19424/com.parkingmore E/SlidingMenu: Dispatchtouchevent ev:206-08 09:52:08.495 19424-19424/com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.515 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:206-08 09:52:08.515 19424-19424/ Com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.533 19424-19424/com.parkingmore E/SlidingMenu: Dispatchtouchevent ev:206-08 09:52:08.533 19424-19424/com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.551 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:206-08 09:52:08.551 19424-19424/com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.574 19424-19424/com.parkingmore E/SlidingMenu: Dispatchtouchevent ev:206-08 09:52:08.574 19424-19424/com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.594 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:206-08 09:52:08.595 19424-19424/ Com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.611 19424-19424/com.parkingmore E/SlidingMenu: Dispatchtouchevent ev:206-08 09:52:08.612 19424-19424/com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.622 19424-19424/com.parkingmore e/slidingmenu:dispatchtouchevent ev:206-08 09:52:08.622 19424-19424/ Com.parkingmore e/slidingmenu:ontouchevent ev:206-08 09:52:08.623 19424-19424/com.parkingmore E/SlidingMenu: Dispatchtouchevent ev:1
View Code

As can be seen from log, it is also possible to pass a sliding event to Viewpager, which is then intercepted by Slidingmenu. This log confirms that the conjecture is correct.

Know the reason to solve, I now have to consider is: to achieve what effect.

Expected effect: You can slide the carousel normally, and when the carousel is on the first slide, slide to the side-slip menu.

Someone on the internet has shared a similar problem. I have borrowed a bit of it here.

The first function is to realize that the carousel diagram can be slid normally.

You can set a global variable: public static Boolean mrollviewpagertouching;

Used to indicate whether the carousel is in touch.

In the custom Viewpager, decide whether to touch the carousel diagram

 Public Booleandispatchtouchevent (motionevent ev) {Switch(Ev.getaction ()) { CaseMotionevent.action_move: Break;  CaseMotionEvent.ACTION_DOWN:MyApplication.mRollViewPagerTouching=true;  Break;  CaseMotionEvent.ACTION_UP:MyApplication.mRollViewPagerTouching=false;  Break; }        return Super. dispatchtouchevent (EV); }
View Code

In Slidingmenu, make the following treatment

 Public Boolean onintercepttouchevent (motionevent ev) {        if  (myapplication.mrollviewpagertouching) {             returnfalse;        }         return Super . onintercepttouchevent (EV);    }
View Code

Recompile runs, you can slide the carousel normally, but when the carousel is on the first one, you cannot slide to the side-slip menu.

Now for the second function: When the carousel is on the first slide, you can slide to the side-slip menu.

Here are two places to note: 1. When the carousel is on the first, 2. Since my slide-in menu is on the left, I need the finger to slide right into the side-slip menu, so the second condition should be the right slide.

Based on the above, modify the custom Viewpager code

 Public Booleandispatchtouchevent (motionevent ev) {//go back to the starting coordinates when triggered        floatx =Ev.getx (); Switch(Ev.getaction ()) { CaseMotionevent.action_move://get to distance difference                floatDX = x-Downx; //prevention is also judged by the press                if(Math.Abs (DX) > 8) {                    //Judging direction by distance difference                    if(DX > 0) {                        //"right";                        if(Getcurrentitem () = = 0) {myapplication.mrollviewpagertouching=false; } Else{myapplication.mrollviewpagertouching=true; }                    } Else {                        //"left";Myapplication.mrollviewpagertouching =true; }                }                 Break;  CaseMotionevent.action_down://Store the coordinates when pressedDownx =x; Myapplication.mrollviewpagertouching=true;  Break;  CaseMotionEvent.ACTION_UP:MyApplication.mRollViewPagerTouching=false;  Break; }        return Super. dispatchtouchevent (EV); }
View Code

Compile and run again to achieve the desired results.

PS: simple function, simple sharing.

Sliding Collision of Android slide menu and Carousel diagram

Related Article

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.