通过手势切换Fragment
2015-11-23 00:38
260 查看
上篇文章讲到图片的自动切换,这篇讲下怎么通过手势来切换Fragment,就像微信一样,既可以点击底部的四个按钮来切换,也可以通过手势的滑动来切换。如图
![](http://img.blog.csdn.net/20151123003121682?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
手势检测用到了GestureDetector类,GestureDetector实例代表一个手势检测器,创建GestureDetector时需要传入一个GestureDetector.OnGestureListener实例,GestureDetector.OnGestureListener是一个监听器,负责对用户的手势行为提供响应。
GestureDetector.OnGestureListener里面包含的事件处理方法如下:
boolean onDown(MotionEvent e):当触碰事件按下时触发该方法。
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY):当用户在触摸屏上拖过时触发该方法,其中velocityX,velocityX代表拖过动作在横向纵向上的速度。
onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法。
onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY):当用户在屏幕上滚动时触发该方法。
onShowPress(MotionEvent e):当用户在触屏上按下,而且还没有移动和松开时触发该方法。
onSingleTapUp(MotionEvent e):用户在触摸屏上的轻击事件将会触发该方法。
下面是代码:
Demo下载地址:http://download.csdn.net/detail/barryyanggoing/9291693
手势检测用到了GestureDetector类,GestureDetector实例代表一个手势检测器,创建GestureDetector时需要传入一个GestureDetector.OnGestureListener实例,GestureDetector.OnGestureListener是一个监听器,负责对用户的手势行为提供响应。
GestureDetector.OnGestureListener里面包含的事件处理方法如下:
boolean onDown(MotionEvent e):当触碰事件按下时触发该方法。
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY):当用户在触摸屏上拖过时触发该方法,其中velocityX,velocityX代表拖过动作在横向纵向上的速度。
onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法。
onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY):当用户在屏幕上滚动时触发该方法。
onShowPress(MotionEvent e):当用户在触屏上按下,而且还没有移动和松开时触发该方法。
onSingleTapUp(MotionEvent e):用户在触摸屏上的轻击事件将会触发该方法。
下面是代码:
public class MainActivity extends FragmentActivity implements OnClickListener, OnGestureListener { private TextView txtHome, txtSearch, txtFriend, txtMe; private GestureDetector gestureDetector; private static final int FLING_MIN_DISTANCE = 50; private static final int FLING_MIN_VELOCITY = 0; private SharedPreferences preferences; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_main); gestureDetector = new GestureDetector(this, this); //记录滑动前在哪个Fragment preferences = getSharedPreferences("local",Context.MODE_PRIVATE); preferences.getInt("localFlag", 0); initView(); initOnclick(); if (bundle == null) { mainTabUtil(new HomeFragment()); } } private void initOnclick() { txtHome.setOnClickListener(this); txtSearch.setOnClickListener(this); txtFriend.setOnClickListener(this); txtMe.setOnClickListener(this); } private void initView() { txtHome = (TextView) findViewById(R.id.txtHome); txtSearch = (TextView) findViewById(R.id.txtSearch); txtFriend = (TextView) findViewById(R.id.txtFriend); txtMe = (TextView) findViewById(R.id.txtMe); } @Override public void onClick(View v) { SharedPreferences.Editor editor = preferences.edit(); switch (v.getId()) { case R.id.txtHome: mainTabUtil(new HomeFragment()); editor.putInt("localFlag", 0); editor.commit(); break; case R.id.txtSearch: mainTabUtil(new FindFragment()); editor.putInt("localFlag", 1); editor.commit(); break; case R.id.txtFriend: mainTabUtil(new FriendFragment()); editor.putInt("localFlag", 2); editor.commit(); break; case R.id.txtMe: mainTabUtil(new MeInfoFragment()); editor.putInt("localFlag", 3); editor.commit(); break; default: break; } } public void mainTabUtil(Fragment fragment) { getSupportFragmentManager().beginTransaction() .replace(R.id.content_frame, fragment).commit(); } @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { //Toast.makeText(this, "触碰事件按下时触发onDown()", 0).show(); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { int localFlag = preferences.getInt("localFlag", 0); if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { //向左 turnToFragment(localFlag,"left"); } else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { //向右 turnToFragment(localFlag,"right"); } return false; } private void turnToFragment(int localFlag,String direction ) { //向右边 SharedPreferences.Editor editor = preferences.edit(); if(direction.equals("right")){ if(localFlag == 0){ mainTabUtil(new FindFragment()); editor.putInt("localFlag", 1); editor.commit(); }else if(localFlag == 1){ mainTabUtil(new FriendFragment()); editor.putInt("localFlag", 2); editor.commit(); }else if(localFlag == 2){ mainTabUtil(new MeInfoFragment()); editor.putInt("localFlag", 3); editor.commit(); } }else if(direction.equals("left")){ if(localFlag == 3){ mainTabUtil(new FindFragment()); editor.putInt("localFlag", 2); editor.commit(); }else if(localFlag == 2){ mainTabUtil(new FriendFragment()); editor.putInt("localFlag", 1); editor.commit(); }else if(localFlag == 1){ mainTabUtil(new HomeFragment()); editor.putInt("localFlag", 0); editor.commit(); } } } @Override public void onLongPress(MotionEvent e) { //Toast.makeText(this, "长按时触发onLongPress()", 0).show(); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { //Toast.makeText(this, "当用户在屏幕上滚动时触发onScroll()", 0).show(); return false; } @Override public void onShowPress(MotionEvent e) { //Toast.makeText(this, "当用户在触屏上按下,还未移动和松开时触动onShowPress()", 0).show(); } @Override public boolean onSingleTapUp(MotionEvent e) { //Toast.makeText(this, "用户在触摸屏上的轻击事件触发onSingleTapUp()", 0).show(); return false; } }
Demo下载地址:http://download.csdn.net/detail/barryyanggoing/9291693
相关文章推荐
- 《算法导论》读书笔记--第三章 函数的增长
- Photoshop CS6 自学(二)图像知识
- 等待锁和等待条件、临界区实现、读写锁实现
- 【编程】超快速计算平方根的倒数
- Git中工作区与暂存区的区别
- 为什么String类的值是常量,但内容可以修改?
- Android异常处理——try、catch、finally、throw、throws
- 《Trigger》第三篇:cocos2d-x中json文件创建、读取和写入
- 线段树入门(转!!!!)
- 摇一摇切图.apk
- rpc 小例子
- git常用命令
- 互联网及相关公司的求职经历分享
- leetcode之Implement Trie (Prefix Tree)
- 字符串转化为数学表达式并求值(后缀表达式)
- 如何修改eclipse中@author的默认选项
- Oracle清空或者删除当前用户下所有的表
- U盘自动复制文件
- 草莓派入门(八)
- 几种基于HTTP协议的RPC性能比较