問題描述
我正在動態地將圖像視圖添加到父布局.我正在對添加的圖像執行放大/縮小操作.我想刪除它的添加視圖 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中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!