Android 在活动之间滑动

Android 在活动之间滑动,android,android-activity,fragment,swipe,Android,Android Activity,Fragment,Swipe,我有两个活动,我想通过滑动在它们之间切换,我在google上做了很多研究,但找不到解决方案,因为我正在使用位图(图像),我在活动的onCreate()方法中编写了大部分代码,有没有解决方案,或者如何将活动转换为片段有一些库供您使用: 您可以使用GestureDetector执行此操作。下面是示例代码段 // You can change values of below constants as per need. private static final int MIN_DISTANCE

我有两个活动,我想通过滑动在它们之间切换,我在google上做了很多研究,但找不到解决方案,因为我正在使用位图(图像),我在活动的onCreate()方法中编写了大部分代码,有没有解决方案,或者如何将活动转换为片段

有一些库供您使用:


  • 您可以使用GestureDetector执行此操作。下面是示例代码段

    // You can change values of below constants as per need.
    private static final int MIN_DISTANCE = 100;
    private static final int MAX_OFF_PATH = 200;
    private static final int THRESHOLD_VELOCITY = 100;
    private GestureDetector mGestureDetector;
    
    // write below code in onCreate method
    mGestureDetector = new GestureDetector(context, new SwipeDetector());
    
    // Set touch listener to parent view of activity layout
    // Make sure that setContentView is called before setting touch listener.
    findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // Let gesture detector handle the event
                    return mGestureDetector.onTouchEvent(event);
                }
            });
    
    
    // Define a class to detect Gesture
    private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (e1 != null && e2 != null) {
                    float dy = e1.getY() - e2.getY();
                    float dx = e1.getX() - e2.getX();
    
                    // Right to Left swipe
                    if (dx > MIN_DISTANCE && Math.abs(dy) < MAX_OFF_PATH &&
                            Math.abs(velocityX) > THRESHOLD_VELOCITY) {
                // Add code to change activity  
                        return true;
                    }
    
                    // Left to right swipe
                    else if (-dx > MIN_DISTANCE && Math.abs(dy) < MAX_OFF_PATH &&
                            Math.abs(velocityX) > THRESHOLD_VELOCITY) {
                // Below is sample code to show left to right swipe while launching next activity
               currentActivity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
               startActivity(new Intent(currentActivity,NextActivity.class));
                        return true;
                    }
                }
                return false;
            }
    }
    
    //Below are sample animation xml files.
    
    anim/right_in.xml
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="500"
            android:fromXDelta="-100%p"
            android:toXDelta="0" />
    </set>
    
    anim/right_out.xml
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="500"
            android:fromXDelta="0"
            android:toXDelta="100%p" />
    </set>
    
    //您可以根据需要更改以下常量的值。
    专用静态最终整数最小距离=100;
    专用静态最终int MAX_OFF_PATH=200;
    私有静态最终整数阈值_速度=100;
    私人手势检测器mGestureDetector;
    //在onCreate方法中编写以下代码
    mGestureDetector=新的手势检测器(上下文,新的SwipedDetector());
    //将touch listener设置为活动布局的父视图
    //确保在设置touch listener之前调用了setContentView。
    findviewbyd(R.id.parent_视图).setOnTouchListener(新视图.OnTouchListener(){
    @凌驾
    公共布尔onTouch(视图v,运动事件){
    //让手势检测器处理事件
    返回mGestureDetector.onTouchEvent(事件);
    }
    });
    //定义一个类来检测手势
    私有类SwipeDetector扩展了GestureDetector.SimpleOnGestureListener{
    @凌驾
    公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
    如果(e1!=null&&e2!=null){
    float dy=e1.getY()-e2.getY();
    float dx=e1.getX()-e2.getX();
    //从右向左滑动
    如果(dx>最小距离和&Math.abs(dy)阈值(速度){
    //添加代码以更改活动
    返回true;
    }
    //从左向右滑动
    否则如果(-dx>最小距离和&Math.abs(dy)阈值(速度){
    //下面是启动下一个活动时从左向右滑动的示例代码
    currentActivity.overridePendingTransition(R.anim.right\u in,R.anim.right\u out);
    startActivity(新意图(currentActivity,NextActivity.class));
    返回true;
    }
    }
    返回false;
    }
    }
    //下面是示例动画xml文件。
    anim/right_in.xml
    anim/right_out.xml
    
    为什么不在ViewPager中使用片段?我创建了一个活动来显示SD卡图像并从中进行选择,这样我就可以在另一个活动(编辑活动)中显示它们,在那里我可以编辑它们,我不知道我是否可以像现在一样将活动转换为片段转换并不困难,只要在活动中稍作改变就可以了!Good luckIt运行良好,谢谢,但我想看到的是幻灯片效果和活动已经打开,在所有这些尝试之后,我正在考虑将每个活动转换为片段,再次感谢!对于幻灯片效果,您可以使用动画和活动的overridePendingTransition方法。请检查我的更新答案。但是,是的,我同意使用片段是正确的方法。我已经达到了使用片段的目的,再次感谢您的回答!