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组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...
随机推荐
- JAVA Useful Program(1)
public static void main(String[] args){ //字符串有整型的相互转换 String str=String.valueOf(123); ...
- dwz框架---(2)表单回调函数
dwz中的表单回调函数大概有下面几种: /** * 普通ajax表单提交 * @param {Object} form * @param {Object} callback * @param {Str ...
- Linux入门基础#2:Linux文件系统基本结构
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- OpenCv调用摄像头拍照代码
近期在研究OpenCv对摄像头的调用.现将代码贴出,供大家批评指正. 1.申明 #include"./opencv2/opencv.hpp" #ifdef _DEBUG #prag ...
- jquery中怎么删除<ul>中的整个<li>包括节点
.$('ul li').remove(); .$('ul li').each(function(){ $(this).remove(); }); .$("ul").find(&qu ...
- thinkPHP框架介绍(一)
原文:thinkPHP框架介绍(一) 一.ThinkPHP的介绍 期间有对ThinkPHP框架在学习上的问题欢迎大家交流:QQ:812231134 MVC M - Model 模型 ...
- Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计)
Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计) 本篇博客介绍Cocos2d-x的第一个实例--重力加速计測试.效果图(注:这里无法模拟重力感应): --[ ...
- Managing Data in Containers
Managing Data in Containers So far we've been introduced to some basic Docker concepts, seen how to ...
- 灰度共生矩阵(GLCM) 及matlab代码实现
原地址:http://blog.csdn.net/bookwormno1/article/details/7962466 这几天学习灰度共生矩阵,现记录如下: 讲灰度共生矩阵比较好的一份百度文库文档: ...
- Wamp环境下配置--Apache虚拟主机
1.首先打开apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#,启用虚拟主机功能 # Virtual hosts In ...