android tab之间滑动切换界面功能
2015-01-15 10:16
232 查看
http://www.cnblogs.com/lianghui66/archive/2013/07/25/3213944.html
1. onTouchListener(); //捕捉touch事件,比如说onDown
需要将可滑动的控件加上两个方法:(1)view.setOnTouchListener(); //实现可以touch
(2) view.setLongClickAble(); //如果不加这个方法,这个view只会接受onDown()点击事件。onFling() onScroll()等方法不接受
此方法需要注意,其目的是接收控件的touch事件,哪需要就要在哪加上。比如说最外面的Layout,中间的ListView,尤其注意当有ScrollView时一定要给它也加上这个方法,否则ScrollView里面的控件会不接受onFling()方法。
2. GestureDetector //手势识别
其中我们要使用的是继承了GestureDetector.onDoubleTapLisener和GestureDetector.OnGestureListener的GestureDetector.SimpleOnGestureListener。其中重写onFling()方法。此方法是在快速滑动屏幕时才会执行,正好符合我们的功能。
中间我们要把自定义的GestureDetector类与控件的onTouch()方法关联起来。在Activity中实现View.OnTouchListener(),重写它的方法:
GestureDetector detector = new GestureDetector(new MySimpleGestureDetector());
public void onTouch(View view, MontionEvent event){
return detector.onTouchEvent(event); //关联
}
方法体如下:附注释
public class MySimpleGestureDetector extends GestureDetector.SimpleOnGestureListener {
private static final int MIN_DISTANCE = 100; //最小距离
private static final int MIN_VELOCITY = 100; //最小滑动速率
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(velocityX) > MIN_VELOCITY) {
if ((e2.getX() - e1.getX()) > MIN_DISTANCE) { //向右滑动
TabActivity.flingRight();
} else if ((e1.getX() - e2.getX()) > MIN_DISTANCE) { //向左滑动
TabActivity.flingLeft();
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
3. 此时所有支持滑动的控件都加上了touch监听事件,并关联到自定义的SimpleGestureDetector里。并且在自定义的SimpleGestureDetector中重写的onFling()方法,处理了左右快速滑动操作。滑动最小距离为100px,X轴上滑动最小速率为100px/s。所以最后一步就是在你的TabActivity中处理左右滑动就可以了。附代码:
public static void flingLeft() {
int currentTab = tabHost.getCurrentTab();
if (currentTab != 0) {
currentTab--;
switchTab(currentTab);
}
}
public static void flingRight() {
int currentTab = tabHost.getCurrentTab();
if (currentTab != tabHost.getTabWidget().getChildCount()) {
currentTab++;
switchTab(currentTab);
}
}
private static void switchTab(final int toTab) {
new Thread(new Runnable() {
@Override
public void run() {
tabHost.post(new Runnable() {
@Override
public void run() {
tabHost.setCurrentTab(toTab);
}
});
}
}).start();
}
这样一个支持左右滑动切换界面的Tab就做好了。
1. onTouchListener(); //捕捉touch事件,比如说onDown
需要将可滑动的控件加上两个方法:(1)view.setOnTouchListener(); //实现可以touch
(2) view.setLongClickAble(); //如果不加这个方法,这个view只会接受onDown()点击事件。onFling() onScroll()等方法不接受
此方法需要注意,其目的是接收控件的touch事件,哪需要就要在哪加上。比如说最外面的Layout,中间的ListView,尤其注意当有ScrollView时一定要给它也加上这个方法,否则ScrollView里面的控件会不接受onFling()方法。
2. GestureDetector //手势识别
其中我们要使用的是继承了GestureDetector.onDoubleTapLisener和GestureDetector.OnGestureListener的GestureDetector.SimpleOnGestureListener。其中重写onFling()方法。此方法是在快速滑动屏幕时才会执行,正好符合我们的功能。
中间我们要把自定义的GestureDetector类与控件的onTouch()方法关联起来。在Activity中实现View.OnTouchListener(),重写它的方法:
GestureDetector detector = new GestureDetector(new MySimpleGestureDetector());
public void onTouch(View view, MontionEvent event){
return detector.onTouchEvent(event); //关联
}
方法体如下:附注释
public class MySimpleGestureDetector extends GestureDetector.SimpleOnGestureListener {
private static final int MIN_DISTANCE = 100; //最小距离
private static final int MIN_VELOCITY = 100; //最小滑动速率
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(velocityX) > MIN_VELOCITY) {
if ((e2.getX() - e1.getX()) > MIN_DISTANCE) { //向右滑动
TabActivity.flingRight();
} else if ((e1.getX() - e2.getX()) > MIN_DISTANCE) { //向左滑动
TabActivity.flingLeft();
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
3. 此时所有支持滑动的控件都加上了touch监听事件,并关联到自定义的SimpleGestureDetector里。并且在自定义的SimpleGestureDetector中重写的onFling()方法,处理了左右快速滑动操作。滑动最小距离为100px,X轴上滑动最小速率为100px/s。所以最后一步就是在你的TabActivity中处理左右滑动就可以了。附代码:
public static void flingLeft() {
int currentTab = tabHost.getCurrentTab();
if (currentTab != 0) {
currentTab--;
switchTab(currentTab);
}
}
public static void flingRight() {
int currentTab = tabHost.getCurrentTab();
if (currentTab != tabHost.getTabWidget().getChildCount()) {
currentTab++;
switchTab(currentTab);
}
}
private static void switchTab(final int toTab) {
new Thread(new Runnable() {
@Override
public void run() {
tabHost.post(new Runnable() {
@Override
public void run() {
tabHost.setCurrentTab(toTab);
}
});
}
}).start();
}
这样一个支持左右滑动切换界面的Tab就做好了。
相关文章推荐
- android tab之间滑动切换界面功能
- android tab之间滑动切换界面功能
- Android实现界面左右滑动切换功能
- android界面的滑动切换功能
- android-使用ViewGroup实现左右滑动切换界面的效果
- virtualbox安装增强功能:鼠标在主机与虚拟机之间自由切换
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- android-运用ViewGroup完成左右滑动切换界面以及界面裂开的作用
- Android学习笔记之不同activity之间滑动切换
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- ViewPager滑动切换界面
- VC多对话框界面程序,界面之间的切换问题
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- InstallShield 2012 Spring新功能试用(18): Suite/Advanced UI 和 Advanced UI工程的界面编辑视图中添加了控件工具栏和语言切换功能
- 模仿QQ的左右滑动切换界面和下拉更新的效果
- 多个layout界面之间的切换
- 带有ListView的界面左右滑动,切换界面。
- 【Android】两个layout之间的滑动切换