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