Tips for Android Event distribution

Source: Internet
Author: User

@Override public boolean OnTouch (View V, Final motionevent event) {Checkablebutton cb = (Checkablebutton) v;if (CB. Iseditable()) {int action = Event. Getaction();Rect outrect = new Rect ();Getactivity (). GetWindow(). Findviewbyid(Window. ID_android_content). Getdrawingrect(Outrect);ScreenWidth = Outrect. Width();ScreenHeight = Outrect. Height();Switch (action) {case motionevent. ACTION_down:startx = V. GetLeft();Starty = V. GetTop();EndX = V. GetRight();EndY = V. Getbottom();Log. I(TAG,"Action_down:startx ="+ StartX);Log. I(TAG,"Action_down:starty ="+ starty);LASTX = (int) event. GETRAWX();Lasty = (int) event. Getrawy();                     Break;Case Motionevent. ACTION_move:v. GetParent(). Requestdisallowintercepttouchevent(true);if (islongclickmodule) {int dx = (int) event. GETRAWX()-Lastx;int dy = (int) event. Getrawy()-Lasty;Log. I(TAG,"ONTOUCH:DX ="+ dx);Log. I(TAG,"Ontouch:dy ="+ dy);int left = V. GetLeft() + DX;int top = V. GetTop() + dy;int right = V. GetRight() + DX;int bottom = V. Getbottom() + dy;if (left <0) {left =0;right = left + V. GetWidth();} if (right > ScreenWidth) {right = ScreenWidth;left = Right-v. GetWidth();} if (Top <0) {top =0;Bottom = top + V. GetHeight();} if (Bottom > screenheight) {bottom = ScreenHeight;top = Bottom-v. GetHeight();} V. Layout(left, top, right, bottom);LASTX = (int) event. GETRAWX();Lasty = (int) event. Getrawy();Dealing with bottomfragment float px = getresources (). Getdimension(R. Dimen. Bottomfrag_bar_height);int heightpixels = getactivity (). Getresources(). Getdisplaymetrics(). Heightpixels;if (Lasty > heightpixels-px) {mcallbacks. Ondragtoloction(Getresources (). getString(R. String. FA_trash_o));IsDeleted = True;}else{Mcallbacks. Ondragtoloction(Getresources (). getString(R. String. FA_floppy_o));IsDeleted = False;} Log. I(TAG,"ONTOUCH:LASTX ="+ lastx);Log. I(TAG,"ontouch:lasty ="+ lasty);} Break;Case Motionevent. ACTION_up:v. Layout(StartX, Starty, EndX, EndY);Islongclickmodule = False;if (isDeleted) {DELETECB (CB);} Log. I(TAG,"action_up:scrolly ="+ Mscrollview. getscrolly());Timer. Cancel();                     Break;This event is triggered when your finger (or other) moves the screen, such as when your finger drags a ListView or a scrollview on the screen instead of pressing the button above to trigger the event//Because the trigger starts at the root of the view tree, so Add v when you move.. GetParent(). Requestdisallowintercepttouchevent(true); this sentenceCase Motionevent. ACTION_cancel:islongclickmodule = False;V. Layout(StartX, Starty, EndX, EndY);if (isDeleted) {DELETECB (CB);}//timer. Cancel();                     Break;}} return False;}

1. 当ontouch return false的时候onTouch执行后会继续执行Onclick,onLongClick之类的函数,return true时就不会执行了,所以ACTION_DOWN的优先级是高于click的
2. 事件的派发总是自顶向下即先父布局在子布局,如果不想让父布局接受事件需要在恰当的位置添加v.getParent().requestDisallowInterceptTouchEvent(true);这样一句话
3.当你的手指(或者其它)移动屏幕的时候会触发这个事件,比如当你的手指在屏幕上拖动一个listView或者一个ScrollView而不是去按上面的按钮时会触发这个事件。

Tips for Android event distribution

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.