1、因为Android界面上的全部控件一般都位于Layout控件(比方RelativeLayout)之上,而布局控件能够设置响应touch事件,所以能够通过布局控件的setOnTouchListen来截获touch事件。做进一步的处理。

2、关于界面滑动。涉及到gesture的处理,而gesture(手势)是touch事件的更高一层的事件,能够将touch事件传入GestureDetector对象进行处理,而创建GestureDetector对象,要首先创建OnGestureListener对象,在OnGestureListener的OnFling函数中能够进行手势识别。

3、详细流程。

实现OnTouchListen和OnGestureListen两个抽象类,同一时候实现当中的抽象函数就可以。

(1)IntellisenseActivity中继承抽象类

(2)创建布局控件RelativeLayout的id为relativelayout

(3)代码编写

<span style="font-size:14px;">package com.design.cjc.intellisense;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout; public class IntellisenseActivity extends ActionBarActivity implements View.OnTouchListener,GestureDetector.OnGestureListener{ private RelativeLayout rl;
private GestureDetector gd; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intellisense); rl=(RelativeLayout)findViewById(R.id.relativelayout);
rl.setOnTouchListener(this);
rl.setLongClickable(true); //非常重要
gd=new GestureDetector((GestureDetector.OnGestureListener)this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_intellisense, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
} @Override
public boolean onDown(MotionEvent e) {
return false;
} @Override
public void onShowPress(MotionEvent e) { } @Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
} @Override
public void onLongPress(MotionEvent e) { } @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//手势识别
//e1:起点信息
//e2:终点信息
//velocityX:x方向移动速度
//velocityY:y方向移动速度
final int FLING_MIN_DISTANCE=100;
final int FLING_MIN_VELOCITY=200; if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY){
Intent intent = new Intent(IntellisenseActivity.this, PCCtrlActivity.class);
startActivity(intent);
} return false;
} @Override
public boolean onTouch(View v, MotionEvent event) {
return gd.onTouchEvent(event); //touch事件交给GestureDetector处理
//return false;
}
}
</span>

(3)PCCtrlActivity中做与IntellisenseActivity相近的处理

package com.design.cjc.intellisense;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout; public class PCCtrlActivity extends ActionBarActivity implements View.OnTouchListener,GestureDetector.OnGestureListener { private RelativeLayout rl;
private GestureDetector gd; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pcctrl); rl=(RelativeLayout)findViewById(R.id.relativelayout);
rl.setOnTouchListener(this);
rl.setLongClickable(true); //非常重要
gd=new GestureDetector((GestureDetector.OnGestureListener)this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pcctrl, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
} @Override
public boolean onDown(MotionEvent e) {
return false;
} @Override
public void onShowPress(MotionEvent e) { } @Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
} @Override
public void onLongPress(MotionEvent e) { } @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int FLING_MIN_DISTANCE=100;
final int FLING_MIN_VELOCITY=200; if(e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY){
Intent intent = new Intent(PCCtrlActivity.this,IntellisenseActivity.class);
startActivity(intent);
} return false;
} @Override
public boolean onTouch(View v, MotionEvent event) {
return gd.onTouchEvent(event);
}
}

參考文献

參考文献1

參考文献2

最新文章

  1. DOM 概况
  2. POJ 3903
  3. [收藏]Asp.net MVC生命周期
  4. HTML 5 History API的”前生今世”
  5. javascript中的undefined,null,&quot;&quot;,0和false的云集
  6. [置顶] STM32移植contiki进阶之三(中):timer 中文版
  7. 怎么样MyEclipse配置Tomcat?
  8. ASP.NET MVC页面UI之联动下拉选择控件(省、市、县联动选择)
  9. python 文件夹操作
  10. 在区块链侧链上进行Dapp技术开发
  11. mac Robotframework执行时报错Robot Framework installation not found.
  12. Keepalived详解(四):通过vrrp_script实现对集群资源的监控【转】
  13. Add Columns to the Web Sessions List
  14. 移植U-Boot时遇到的问题
  15. HDU3567
  16. [转]10+倍性能提升全过程--优酷账号绑定淘宝账号的TPS从500到5400的优化历程
  17. React半科普文
  18. 初学Python-搞了一个linux用户登录监测小工具
  19. [转] Hadoop MapReduce开发最佳实践(上篇)
  20. Ubuntu 13.04 安装 Oracle11gR2

热门文章

  1. HDU1251 统计难题 trie树 简单
  2. 20162327WJH四则运算第二周总结
  3. 扩展gcd codevs 1200 同余方程
  4. ZOJ 3213 Beautiful Meadow 简单路径 插头DP
  5. noip模拟 五子棋
  6. c# -- 介绍File.AppendAllText 方法
  7. Redis-用思维导图二天搞定Redis用法。
  8. shell之if简化语句
  9. React事件初探
  10. jdbc框架有很多,包括spring jdbc