安卓:轻扫屏幕来打开另一个活动? [英] Android: swipe screen to open another activity?

查看:134
本文介绍了安卓:轻扫屏幕来打开另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

只是为了教程的目的,我想使简单的菌群失调和放大器;动物(植物和放大器;动物)的百科全书

我想让我的主屏幕拖拽就能就像Android的主屏幕。向右滑动来打开工厂页面,并向左滑动打开动物页面。 我不知道如何使过渡效果。因此,我们可以将它拖到中途偷看什么在接下来的页面,只需将回取消

你们可以共享一个链接,使拖能屏?

谢谢

@Agarwal 我试过code从链路2和它不工作

我尝试测试手势是否检测或不把吐司内部类中,但敬酒不显示。该链接1基本上是相同的,但。

和从的code外观上来看,我认为它不能让我的屏幕拖能像Android的主屏幕

我的code:

 公共类家庭延伸活动实现OnClickListener {
    私人GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    ImageButton的植物,动物;
    意图去;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    初始化();

    gestureDetector =新GestureDetector(新SwipeGestureDetector());
    gestureListener =新View.OnTouchListener(){
        公共布尔onTouch(视图V,MotionEvent事件){
            返回gestureDetector.onTouchEvent(事件);
        }
    };
}

私人无效初始化(){
    //发现鉴于ID为图像按钮
    //设置onClickListener图像按钮
}

公共无效的onClick(视图v){
    //为每个按钮正常开关和案例

}

私人无效onLeftSwipe(){
    吐司T = Toast.makeText(Home.this,左刷卡,Toast.LENGTH_LONG);
    t.show();
    去=新的意向书(test.apps.FLORA);
    startActivity(GO);
}

私人无效onRightSwipe(){
    吐司T = Toast.makeText(Home.this,右轻扫,Toast.LENGTH_LONG);
    t.show();
    去=新的意向书(test.apps.FAUNA);
    startActivity(GO);
}

私有类SwipeGestureDetector扩展SimpleOnGestureListener {
    私有静态最终诠释SWIPE_MIN_DISTANCE = 50;
    私有静态最终诠释SWIPE_MAX_OFF_PATH = 200;
    私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 200;

    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
            浮动velocityY){
        尝试 {
            吐司T = Toast.makeText(Home.this,手势检测,Toast.LENGTH_SHORT);
            t.show();
            浮diffAbs = Math.abs(e1.getY() -  e2.getY());
            浮动的diff = e1.getX() -  e2.getX();

            如果(diffAbs> SWIPE_MAX_OFF_PATH)
                返回false;

            //左刷卡
            如果(DIFF> SWIPE_MIN_DISTANCE
                    &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                Home.this.onLeftSwipe();
            }
            //右轻扫
            否则,如果(-diff> SWIPE_MIN_DISTANCE
                    &功放;&安培; Math.abs(velocityX)> SWIPE_THRESHOLD_VELOCITY){
                Home.this.onRightSwipe();
            }
        }赶上(例外五){
            Log.e(家,错误的手势);
        }
        返回false;
    }

}
}
 

解决方案

我意识到这是一个老问题,但对于其他人想知道为什么上面的code不起作用那是因为他还没有设置OnTouchListener到查看对象。这就是为什么他的重击事件没有被拾起,因为没有什么是监听了。

他能加入这行来设置自己的形象按钮挥笔(尽管你可能会想要一个更好的视图对象,然后这个):

  flora.setOnTouchListener(gestureListener);
 

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

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

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

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.

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

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

}
}

解决方案

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.

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

flora.setOnTouchListener(gestureListener);

这篇关于安卓:轻扫屏幕来打开另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆