SwipeGesture无法正常工作,因为我想从一个活动刷到另一个活动

时间:2016-06-07 09:58:56

标签: android

我想在不使用Viewpager的情况下从一个活动滑动到另一个活动,我想只使用SwipeGesture我编写了代码但我的屏幕没有滑动所以任何人都可以告诉我代码有什么问题? 这是我的代码:

public class Ashtakoota extends AbstractActivity implements IAPICallback,View.OnClickListener {

    private LinearLayout lnrAsht;

    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLayoutInflater().inflate(R.layout.activity_ashtakoota,mFrameLayout);


        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        lnrAsht = (LinearLayout)findViewById(R.id.lnrAsht);


        lnrAsht.setOnTouchListener(gestureListener);

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


    }



    private class SwipeGestureDetector extends GestureDetector.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.makeText(Ashtakoota.this, "Gesture Detected", Toast.LENGTH_SHORT).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) {
                    onLeftSwipe();
                }
                // Right swipe
                else if (-diff > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    onRightSwipe();
                }
            } catch (Exception e) {
                Log.e("Home", "Error on gestures");
            }
            return false;
        }


    }

    private void onLeftSwipe() {
        Intent intent = new Intent(this,BasicAstro.class);
        startActivity(intent);
    }

    private void onRightSwipe() {
        Intent intent = new Intent(this,MatchManglik.class);
        startActivity(intent);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_match_ashtakootback:{
                Intent intent = new Intent(this,BasicAstro.class);
                startActivity(intent);}break;

            case R.id.btn_match_ashtakootnext:{
                Intent intent = new Intent(this,MatchManglik.class);
                startActivity(intent);}break;
        }
    }

    private boolean setImage(int value1,int value2){


        if (value1 == value2)
            return true;
        else if (value2 >=value1/2)
            return true;
        else
            return false;
    }

}

0 个答案:

没有答案