为填充按钮的衬垫布局添加触摸监听器 [英] adding touch listener for liner layout filled with buttons
问题描述
我在线性布局中添加了一个触摸事件来响应滑动手势,效果很好.但是,当我向布局添加按钮时,父班轮布局被忽略.我应该如何防止这种情况发生?
I added a touch event to a linear layout to respond to swipe gestures, and it works well. However, when i add buttons to the layout, the parent liner layout is ignored. How should I prevent this from happening?
LinearLayout ln2 = (LinearLayout) findViewById(R.id.fr2);
ln2.setOnTouchListener(swipe);
我如何使用onInterceptTouch
?
推荐答案
您应该创建自己的布局并覆盖布局的 onInterceptTouchEvent(MotionEvent ev) 方法.
You should create your own layout and override onInterceptTouchEvent(MotionEvent ev) method of your layout.
例如,我创建了自己的布局,它扩展了 RelativeLayout
For Example I created my own layout which extends 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.
And when i put a button to my layout, even i clicked that button, My Layout consumes all the touchEvent because of the onInterceptTouchEvent
always returns true.
希望对你有帮助
这篇关于为填充按钮的衬垫布局添加触摸监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!