Android(安卓)左右滑动切换页面或Activity的效果实现
16lz
2021-01-26
对于Android桌面可以用手势左右滑动进行切换。或者大家用过iReader的一定被其丰富的手势折服吧。现在看看自己的多个Activy之间如何切换呢
1、需要继承OnGestureListener和OnDoubleTapListener,如下:
Java代码 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0"> 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">- public class ViewSnsActivity extends Activity implements OnTouchListener, OnGestureListener
2、在添加mGestureDetector的定义,并在ViewSnsActivity的onCreate函数中加入其页面布局的setOnTouchListener事件
Java代码 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0"> 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">- GestureDetector mGestureDetector;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.view_sns_activity);
- mGestureDetector = new GestureDetector((OnGestureListener) this);
- LinearLayout viewSnsLayout = (LinearLayout)findViewById(R.id.viewSnsLayout);
- viewSnsLayout.setOnTouchListener(this);
- viewSnsLayout.setLongClickable(true);
- }
3、重载onFling函数
Java代码 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0"> 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- if (e1.getX()-e2.getX() > snsConstant.getFlingMinDistance()
- && Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {
- // 切换Activity
- // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
- // startActivity(intent);
- Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
- } else if (e2.getX()-e1.getX() > snsConstant.getFlingMinDistance()
- && Math.abs(velocityX) > snsConstant.getFlingMinVelocity()) {
- // 切换Activity
- // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
- // startActivity(intent);
- Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
- }
- return false;
- }
其中SnsConstant如下:
Java代码 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0"> 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">- public class SnsConstant {
- private static final int FLING_MIN_DISTANCE = 50;
- private static final int FLING_MIN_VELOCITY = 0;
- public static int getFlingMinDistance() {
- return FLING_MIN_DISTANCE;
- }
- public static int getFlingMinVelocity() {
- return FLING_MIN_VELOCITY;
- }
- }
4、重载onTouch函数
Java代码 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0"> 左右滑动切换页面或Activity的效果实现" style="border:0px;list-style:none;" width="0" height="0">- public boolean onTouch(View v, MotionEvent event) {
- return mGestureDetector.onTouchEvent(event);
- }
更多相关文章
- android实现点击按钮切换不同的fragment布局
- Android中RecyclerView实现横向滑动代码
- 【android】多语言与国际化
- Android(安卓)SlidingMenu使用详解
- schema唤起指定页面
- 利用手势放大图片,并限制图片不超过边界,一般用于大图查看
- Android屏幕切换
- Android实现标题显示隐藏功能
- 【原创】Android仿新浪微博的“更多”页面效果