Android:滑动屏幕打开另一个活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10125512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:35:15  来源:igfitidea点击:

Android: swipe screen to open another activity?

androidgestureswipe-gesture

提问by hrsetyono

i'm a n00b programmer and need a lot of help.

我是一名 n00b 程序员,需要很多帮助。

Just for tutorial purpose, I want to make simple flora & fauna (plant & animal) encyclopedia

仅出于教程目的,我想制作简单的动植物(植物和动物)百科全书

I want to make my home screen drag-able just like the Android's home screen. Swipe right to open Plant page and swipe left to open Animal page. I don't know how to make the transition effect. So we can drag it halfway to peek what's in next page and just drag back to cancel it

我想让我的主屏幕像 Android 的主屏幕一样可拖动。向右滑动打开植物页面,向左滑动打开动物页面。我不知道如何制作过渡效果。所以我们可以把它拖到一半来查看下一页的内容,然后拖回去取消它

Can you guys share a link to make the drag-able screen?

你们可以分享一个链接来制作可拖动的屏幕吗?

Thanks before

之前谢谢

[Edit]

[编辑]

@Agarwal I tried the code from your Link2 and it's not working

@Agarwal 我尝试了您的 Link2 中的代码,但它不起作用

I try to test whether the gesture detected or not by putting Toast inside the inner class but the Toast isn't shown. The Link1 is basically the same though.

我尝试通过将 Toast 放入内部类来测试是否检测到手势,但未显示 Toast。Link1 基本上是一样的。

and from the looks of the code, I think it can't make my screen drag-able like in Android's home screen

从代码的外观来看,我认为它不能像在 Android 的主屏幕中那样使我的屏幕可拖动

my code:

我的代码:

public class Home extends Activity implements OnClickListener {
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    ImageButton flora, fauna;
    Intent go;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initialize();

    gestureDetector = new GestureDetector(new SwipeGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };
}

private void initialize() {
    //find view by id to image button
    //set onClickListener to image button
}

public void onClick(View v) {
    //normal switch and case for each button

}

private void onLeftSwipe() {
    Toast t = Toast.makeText(Home.this, "Left swipe", Toast.LENGTH_LONG);
    t.show();
    go = new Intent("test.apps.FLORA");
    startActivity(go);
}

private void onRightSwipe() {
    Toast t = Toast.makeText(Home.this, "Right swipe", Toast.LENGTH_LONG);
    t.show();
    go = new Intent("test.apps.FAUNA");
    startActivity(go);
}

private class SwipeGestureDetector extends SimpleOnGestureListener {
    private static final int SWIPE_MIN_DISTANCE = 50;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
            Toast t = Toast.makeText(Home.this, "Gesture detected", Toast.LENGTH_SHORT);
            t.show();
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();

            if (diffAbs > SWIPE_MAX_OFF_PATH)
                return false;

            // Left swipe
            if (diff > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Home.this.onLeftSwipe();
            } 
            // Right swipe
            else if (-diff > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Home.this.onRightSwipe();
            }
        } catch (Exception e) {
            Log.e("Home", "Error on gestures");
        }
        return false;
    }

}
}

采纳答案by David Hirst

I realise this is an old question but for anyone else wondering why the above code does not work it is because he has not set the OnTouchListener to a View object. This is why his swipe "event" is not being picked up, because nothing is listening for it.

我意识到这是一个老问题,但是对于其他想知道为什么上面的代码不起作用的人来说,这是因为他没有将 OnTouchListener 设置为 View 对象。这就是为什么他的滑动“事件”没有被接收到的原因,因为没有任何东西在监听它。

He could add this line to set swipes on his image button (though you would probably want a better View object then this):

他可以添加这一行来在他的图像按钮上设置滑动(尽管你可能想要一个更好的 View 对象):

flora.setOnTouchListener(gestureListener);

回答by Manish Dhruw

Android Activity Swipe Detection

Android Activity 滑动检测

Create A Base Activity Class

创建基础活动类

public abstract class _SwipeActivityClass extends AppCompatActivity
{
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        gestureDetector = new GestureDetector( this, new SwipeDetector());
    }

    protected abstract void onSwipeRight();
    protected abstract void onSwipeLeft();

    public class SwipeDetector extends GestureDetector.SimpleOnGestureListener
    {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {

            // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH,
            // then dismiss the swipe.
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            {
                return false;
            }

            //toast( "start = "+String.valueOf( e1.getX() )+" | end = "+String.valueOf( e2.getX() )  );
            //from left to right
            if( e2.getX() > e1.getX() )
            {
                if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
                {
                    onSwipeRight();
                    return true;
                }
            }

            if( e1.getX() > e2.getX() )
            {
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
                {
                    onSwipeLeft();
                    return true;
                }
            }

            return false;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        // TouchEvent dispatcher.
        if (gestureDetector != null)
        {
            if (gestureDetector.onTouchEvent(ev))
            // If the gestureDetector handles the event, a swipe has been
            // executed and no more needs to be done.
            return true;
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        return gestureDetector.onTouchEvent(event);
    }
}

Then extend your Activityfrom _SwipeActivityClass

然后从_SwipeActivityClass扩展您的活动

implement methods onSwipeLeft()and onSwipeRight()to start another activity

实现方法onSwipeLeft()onSwipeRight()以启动另一个活动