我正在动态地将图像视图添加到父布局.我正在对添加的图像执行放大/缩小操作.我想删除它的添加视图 onLongPress.
I'am adding imageviews to parent layout dynamically. And i'am performing zoom in/out operations onTouch of added image. I want to remove the added view onLongPress of it.
img.setOnLongClickListener(longClickAction);
img.setOnTouchListener(touchAction);
长按:
OnLongClickListener longClickAction = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
parentLayout.removeView((ImageView)v);
return false;
}
};
触摸:
OnTouchListener touchAction = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView)v;
//perfrom zoom operation on touch of imageview
zoom(i, event);
return true;
}
};
只有 Touch 事件有效.为什么?我怎么能两者兼得?我哪里出错了?我应该怎么做才能删除添加的视图?请帮我.提前致谢.
Only Touch events are working. Why? How can i have both? Where iam going wrong? What should i do to remove added view? Please help me. Thanks in advance.
onTouch
总是为您的视图调用,因为这是将事件分派到视图的初始状态.当你长按你的视图时,它仍然首先调用 onTouch
并且因为你在 onTouch
中返回 true
(这意味着你已经消费了这个事件并且它不应该被进一步发送)你不会得到 onLongPress
调用.诀窍是在 onTouch
onTouch
is always called for your view since this is the initial state of dispatching the events to the view. When you long press your view this still calls onTouch
first and since you return true
in onTouch
(which means that you've consumed this event and it should not be further dispatched) you won't get onLongPress
called. What will do the trick is returning false
in onTouch
这篇关于onTouch,onLongClick一起在android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!