Android onTouch、OnLongClick、onClick和ScrollView滑动事件冲突
为了实现近期录制的长按,松开手指,结束录制功能。在项目,难道你去走一走会头晕,书写demo为了下一个梳理。
顺便研究android事件调用机制。
先上效果界面:
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_centerInParent="true"
android:background="@drawable/microp_btn_normal" /> </RelativeLayout>
代码:
package com.example.androidtest; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent; public class MainActivity extends Activity { private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn); btn.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View arg0, MotionEvent arg1) {
Log.d("TAG", "onTouch is called.............");
return false;
}
}); btn.setOnLongClickListener(new OnLongClickListener() { @Override
public boolean onLongClick(View arg0) {
Log.d("TAG", "onLongClick is called.............");
return false;
}
}); btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Log.d("TAG", "onClick is called.............");
}
});
} }
在代码中。我们监听了onTouch、onLongClick、onClick事件,当中onTouch和OnLongClick有返回值,onClick没有返回值。
现执行以上代码,长按button。我们能够看到调用顺序为
onTouch->onLongClick->onClick
现将OnTouch的返回值改为true,可发现onLongClick和onClick事件没有调用。
说明onTouch返回true,兴许事件没有传递。
接下来我们看下onTouch详细触发了哪些事件(仅仅长按)
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
//Log.d("TAG", "onTouch is called.............");
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG", "ACTION_DOWN.............");
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "ACTION_MOVE.............");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("TAG", "ACTION_CANCEL.............");
break;
case MotionEvent.ACTION_UP:
Log.d("TAG", "ACTION_UP.............");
break;
default:
break;
}
return false;
}
});
看下日志截图
我仅仅是按住,并未滑动。
我们将onLongClick返回true。能够预见,onClick事件不会被触发。
但ACTION_UP呢?
经验证ACTION_UP被触发了。
接下来我们换下布局文件:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View android:layout_width="match_parent"
android:layout_height="1000dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_gravity="center_horizontal"
android:background="@drawable/microp_btn_normal" /> </LinearLayout>
</ScrollView>
意图非常easy,让我们的button在滚动栏的底部,日志截图
这时,我们的ACTION_UP没被触发,最后触发的是ACTION_CANCLE,并且我按了好几次才让它触发onLongClick事件。大多数仅仅触发了DOWN/MOVE/CANCLE事件。
我怀疑是ScrollView滑动事件与onTouch事件冲突
在onTouch加上一句防冲突代码
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
//Log.d("TAG", "onTouch is called.............");
arg0.getParent().requestDisallowInterceptTouchEvent(true);//通知父控件勿拦截本控件touch事件
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG", "ACTION_DOWN.............");
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "ACTION_MOVE.............");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("TAG", "ACTION_CANCEL.............");
break;
case MotionEvent.ACTION_UP:
Log.d("TAG", "ACTION_UP.............");
btn.setBackgroundResource(R.drawable.microp_btn_normal);
break;
default:
break;
}
return false;
}
});
arg0.getParent().requestDisallowInterceptTouchEvent(true);//通知父控件勿拦截本控件touch事件
这样我们就和我们预期一样。
总结:
由于我的长按录制音频在ScrollView中,没有做onTouch冲突处理,所以出现非常多莫名其妙的问题。
如今要实现我们的要求,仅仅要在onLongClick中录制,ACTION_UP结束就可以。
demo下载:http://download.csdn.net/detail/msl0903/7224487
版权声明:本文博主原创文章。博客,未经同意不得转载。
Android onTouch、OnLongClick、onClick和ScrollView滑动事件冲突的更多相关文章
- Android 解决Gallery下ScrollView滑动事件冲突
在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...
- android 解决ListView点击与滑动事件冲突
如果你的ListView的Item有滑动功能,但又点击Item跳转到其它activity,这样若是在Adapter里面写点击事件是会导致滑动事件获取不到焦点而失效: 解决方法:不要在adapter里面 ...
- 重写ListView解决ListView内部ViewPaper滑动事件冲突问题
非常easy 重写ListView 其它类似问题解决ScrollView嵌套ViewPager出现的滑动冲突问题 http://blog.csdn.net/zhangyiacm/article/det ...
- 李氏滑动事件冲突解决方案 之 处理子ViewGroup的超棒方案
父ViewGroup(CurView) 和 子 ViewGroup(ParentView) 滑动事件冲突解决方案 之 处理子ViewGroup的超棒方案: 子ViewGroup 以 SlipRelat ...
- Android ScrollView 嵌套 ListView、 ListView 嵌套ScrollView Scroll事件冲突解决办法
本人菜鸟一名,最近工作了,开始学习Android. 最近在做项目的时候,UX给了个design,大概就是下拉刷新的ListView中嵌套了ScrollView,而且还要在ScrollView中添加动画 ...
- Android动画及滑动事件冲突解决(转载)
原文链接:http://blog.csdn.net/singwhatiwanna/article/details/38168103 Android开发中动画和事件处理是程序员迈向高手的必经之路,也是重 ...
- webview滑动事件 与内部html左右滑动事件冲突问题的解决办法
最近在做个混合app , 用html做页面,然后通过webview嵌套在activity中,效果是这样: 开始还是比较顺利,增加了菜单退出按钮,返回键页面回退功能,页面加载显示加载图标(在app端实现 ...
- SwipeRefreshLayout与ViewPager滑动事件冲突解决
问题描写叙述: 开发中发现,SwipeRefreshLayout的下拉刷新,与ViewPager开发的banner的左右滑动事件有一点冲突,导致banner的左右滑动不够顺畅. 非常easy在bann ...
- Android Listview中Button按钮点击事件冲突解决办法
今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...
随机推荐
- 开发人员福利!ChromeSnifferPlus 插件正式登陆 Chrome Web Store
今天(2014-10-30)下午,ChromeSnifferPlus 插件正式登陆 Chrome Web Store. 在线安装地址: https://chrome.google.com/websto ...
- VC 绘图技巧--自定义形状图形
自定义形状图形,定义几个点围城的图形,然后进行描边和填充: if (m_memDC.m_hDC!=NULL) { CPoint point[4]; point[0].x=nLeft+(int)(0.1 ...
- Java调用cmd命令 打开一个站点
使用Java程序打开一个站点 近期做了个东西使用SWT技术在一个client程序 须要升级时在提示升级 点击窗口上的一个连接 打开下载网页 花费了我非常长时间 用到了把它记录下来 怕是忘记,须要时能 ...
- 出现异常 child->m_pParent == 0
在cocos2d-x中,能够用CCNode类 自己new一个节点(或是用CCnode::node().create()),当将它作为其它若干item(如button项.sprite项.image项)的 ...
- MySQL如何修改root密码
MySQL修改用户密码 因为长期不登录MySQL数据库,登录时经常忘记root权限密码.本文提供一个在数据库服务器上修改root密码的方法,本文撰写基础是在xp操作系统下进行. 第一步 ...
- (四)SAX方式解析XML数据
SAX方式解析XML数据 文章来源:http://www.cnblogs.com/smyhvae/p/4044170.html 一.XML和Json数据的引入: 通常情况下,每个需要访问网络的应用程 ...
- Linux程序设计学习笔记----多线程编程线程同步机制之相互排斥量(锁)与读写锁
相互排斥锁通信机制 基本原理 相互排斥锁以排他方式防止共享数据被并发訪问,相互排斥锁是一个二元变量,状态为开(0)和关(1),将某个共享资源与某个相互排斥锁逻辑上绑定之后,对该资源的訪问操作例如以下: ...
- 高度关注!国务院对A股发出强烈信号↓
高度关注!国务院对A股发出强烈信号↓http://dwz.cn/2qHBd1郎咸平:中国股市存在一大隐疾 使其成为全球市场的一个另类!http://dwz.cn/2qHBVy一不小心,马云又完成了四场 ...
- A Game of Thrones(19) - Jon
The courtyard rang to the song of swords. Under black wool, boiled leather, and mail, sweat trickled ...
- 浅谈Jquery的使用上篇
一. 1.Jquery是什么?有什么特性? jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取.HTML 元素操作. CSS 操作 .HTML 事 ...