onTouch, onLongClick together in android(onTouch,onLongClick一起在android中)
问题描述
我正在动态地将图像视图添加到父布局.我正在对添加的图像执行放大/缩小操作.我想删除它的添加视图 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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:onTouch,onLongClick一起在android中
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01