我向線性佈局添加了觸摸事件以響應滑動手勢,並且效果很好。但是,當我將按鈕添加到佈局時,父級班輪佈局被忽略。我應該如何防止這種情況發生?添加觸摸偵聽器,用於填充按鈕的線條佈局
LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2);
ln2.setOnTouchListener(swipe);
如何使用onInterceptTouch
?
我向線性佈局添加了觸摸事件以響應滑動手勢,並且效果很好。但是,當我將按鈕添加到佈局時,父級班輪佈局被忽略。我應該如何防止這種情況發生?添加觸摸偵聽器,用於填充按鈕的線條佈局
LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2);
ln2.setOnTouchListener(swipe);
如何使用onInterceptTouch
?
您應該創建自己的佈局並覆蓋佈局的onInterceptTouchEvent(MotionEvent ev)方法。
例如,我創建了自己的佈局延伸的RelativeLayout
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true; // With this i tell my layout to consume all the touch events from its childs
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s",
break;
case MotionEvent.ACTION_MOVE:
//Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s",
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
當我把一個按鈕,我的佈局,即使我點擊該按鈕,我的佈局佔用了所有的TouchEvent因爲onInterceptTouchEvent
總是返回true 。
希望這可以幫助你
添加機器人:的onClick =「tapEvent」在您的佈局要採取的點擊。 通過更改MAX_TAP_COUNT值,您可以使用任意數量的水龍頭。
private long thisTime = 0;
private long prevTime = 0;
private int tapCount = 0;
private static final int MAX_TAP_COUNT = 5;
protected static final long DOUBLE_CLICK_MAX_DELAY = 500;
public void tapEvent(View v){
if (SystemClock.uptimeMillis() > thisTime) {
if ((SystemClock.uptimeMillis() - thisTime) > DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) {
Log.d(TAG, "touch event " + "resetting tapCount = 0");
tapCount = 0;
}
if (tapCount()) {
//DO YOUR LOGIC HERE
}
}
}
private Boolean tapCount(){
if (tapCount == 0) {
thisTime = SystemClock.uptimeMillis();
tapCount++;
} else if (tapCount < (MAX_TAP_COUNT-1)) {
tapCount++;
} else {
prevTime = thisTime;
thisTime = SystemClock.uptimeMillis();
//just incase system clock reset to zero
if (thisTime > prevTime) {
//Check if times are within our max delay
if ((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) {
//We have detected a multiple continuous tap!
//Once receive multiple tap, reset tap count to zero for consider next tap as new start
tapCount = 0;
return true;
} else {
//Otherwise Reset tapCount
tapCount = 0;
}
} else {
tapCount = 0;
}
}
return false;
}
感謝,我會嘗試 –
我已經無法弄清楚如何在我的代碼重寫onInterceptTouchEvent。我是否需要爲每個按鈕執行此操作,還是僅需要父級佈局? –
你應該只爲父母佈局。 如果你可以等半個小時,我可以編輯我的答案。 我需要現在去工作 –