onTouch,onLongClick一起在android中

onTouch, onLongClick together in android(onTouch,onLongClick一起在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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

UIActivityViewControllerCompletionHandler How to?(UIActivityViewControllerCompletionHandler 怎么做?)
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)