This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean onInterceptTouchEvent(MotionEvent event) { | |
int action = event.getAction(); | |
if (action == MotionEvent.ACTION_DOWN) { | |
return false; | |
} else { | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean dispatchTouchEvent(MotionEvent event) { | |
int x = (int) event.getX(); | |
int y = (int) event.getY(); | |
switch (event.getAction) { | |
case MotionEvent.ACTION_DOWN: | |
parent.requestDisallowInterceptTouchEvent(true);//true表示禁止父容器攔截 | |
break; | |
case MotionEvent.ACTION_MOVE: | |
int deltaX = x - mLastX; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//重寫父容器的攔截方法 | |
public boolean onInterceptTouchEvent(MotionEvent event) { | |
boolean intercepted = false; | |
int x = (int) event.getX(); | |
int y = (int) event.getY(); | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN://ACTION_DOWN事件必須返回false,一但攔截後續事件將不能傳遞給子View | |
intercepted = false; | |
break; | |
case MotionEvent.ACTION_MOVE://ACTION_MOVE事件根據需要決定是否攔截 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean dispatchTouchEvent(MotionEvent ev) { | |
boolean consume = false; | |
if (onInterceptTouchEvent(ev)) {//表示此根iewGroup攔截這次的點擊事件 | |
consume = onTouchEvent(ev);//流程交給ViewGroup的onTouchEvent處理 | |
} else {//ViewGroup不攔截此次點擊,分發給子元素去處理 | |
consume = child.dispatchTouchEvent(ev);//子元素繼續分發此次點擊事件,如此反覆到事件最後被處理完畢 | |
} | |
return consume; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean dispatchTouchEvent(MotionEvent ev) { | |
boolean consume = false; | |
if (onInterceptTouchEvent(ev)) { | |
consume = onTouchEvent(ev); | |
} else { | |
consume = child.dispatchTouchEvent(ev); | |
} | |
return consume; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean isConsume = mGestureDetector.onTouchEvent(event); | |
return isConsume; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GestureDetector mGestureDetector = new GestureDetector(this); | |
//解決長按屏幕後無法拖動的現象 | |
mGestureDetector.setIsLongpressEnabled(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GestureDetector mGestureDetector = new GestureDetector(this); | |
//解決長按屏幕後無法拖動的現象mGestureDetector.setIsLongpressEnabled(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MarginLayoutParams params = (MarginLayoutParams) btn.getLayoutParams(); | |
params.leftMargin += 100; | |
btn.requestLayout();// 請求重新對View進行measure、layout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ObjectAnimator.ofFloat(yourView, "translationX", 0, 100).setDuration(100).start(); |
NewerOlder