zoukankan      html  css  js  c++  java
  • Android笔记之监听左右滑动事件

    1.activity监听滑动(只对空白部分监听左右滑动,对组件例如ListView无效)

    package com.example.movetest;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    
    public class MainActivity extends Activity {
    
        private GestureDetector mGestureDetector;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mGestureDetector = new GestureDetector(this, new MyGestureListener());
        }
    
        
        public boolean onTouchEvent(MotionEvent event) {
             return mGestureDetector.onTouchEvent(event);  
        }
    
        class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                if (e1.getX() - e2.getX() > 20) {
                    Log.i("MainActivity", "To Left");
                }
                if (e2.getX() - e1.getX() > 20) {
                    Log.i("MainActivity", "To Right");
                }
                return true;
            }
        }
    }

    Done!

  • 相关阅读:
    P2168 [NOI2015]荷马史诗
    P3195 [HNOI2008]玩具装箱TOY
    P1972 [SDOI2009]HH的项链
    P2339 提交作业usaco
    P3974 [TJOI2015]组合数学
    P2831 愤怒的小鸟
    [校内模拟题4]
    P3952 时间复杂度
    P3531 [POI2012]LIT-Letters
    2019.10.1 qbxt模拟题
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3426619.html
Copyright © 2011-2022 走看看