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

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

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

我想让我的主屏幕可以像Android的主屏幕一样拖动 。 向右滑动即可打开Plant页面,向左滑动即可打开Animal页面。 我不知道如何做出过渡效果 。 因此,我们可以将其拖动到一半以查看下一页中的内容,然后向后拖动以取消它

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

以前感谢

[编辑]

@Agarwal我尝试了你的Link2的代码,它不工作

我尝试通过将Toast放在内部类中但未示出Toast来检测手势是否被检测到。 Link1基本上是相同的。

从代码的外观来看,我认为它不能让我的屏幕像Android的主屏幕一样拖动

我的代码:

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;
    }

}
}

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

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

flora.setOnTouchListener(gestureListener);

在这里输入图像描述

你应该使用ViewPager,请看下面的链接。

http://android-developers.blogspot.in/2011/08/horizo​​ntal-view-swiping-with-viewpager.html

http://developer.android.com/training/implementing-navigation/lateral.html#horizo​​ntal-paging


Android活动滑动检测

创建一个基类活动

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);
    }
}

然后从_SwipeActivityClass扩展MainActivity

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

来源

链接地址: http://www.djcxy.com/p/91223.html

上一篇: Android: swipe screen to open another activity?

下一篇: How to swipe images