滑动左右更改活动

时间:2017-04-24 09:33:11

标签: android swipe-gesture

所以我有一个活动,我有一个导航抽屉。我停用滑动以打开导航抽屉(只有在我点击该菜单的按钮时才会打开)。

现在我想轻扫一下活动(比如iPhone)。

我已经做到了,但我不确定这是否正确。

这是我的代码:

GestureDetectorCompat  mGestureDetector;

EditText etBranche;

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToogle;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modifier_branche);
    setTitle("Mes branches");


    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class
                .getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
    mToogle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);

    mDrawerLayout.addDrawerListener(mToogle);
    mToogle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationView navigation = (NavigationView)findViewById(R.id.menu_navigation);

    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {

            switch (item.getItemId())
            {   //si c'est "Mes cours" qui a été choisi
                case R.id.mes_cours:
                    Intent cours = new Intent(ModifierBranche.this,MesCours.class);
                    startActivity(cours);
                    return true;
                case R.id.mes_branches:
                    /
                    finish();
                    startActivity(getIntent());
                    return true;
            }
            return true;

        }
    });
    mGestureDetector = new GestureDetectorCompat(this, new SwipeGestureDetector());

    Intent intent = this.getIntent();
    //si on a réussi à récupérer un intent on fait les tests suivants
    if(intent != null)
    {

        if (option.equals("modifier"))
        {

                String nombranche = intent.getExtras().getString("branche_nom");
                EditText _nombranche = (EditText) findViewById(R.id.ETBranche);
                _nombranche.setText(nombranche);
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //je rajoute le menu que j'ai créé
    getMenuInflater().inflate(R.menu.menu_page_affichage,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(mToogle.onOptionsItemSelected(item))
    {
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawer(Gravity.LEFT);
        } else {
            mDrawerLayout.openDrawer(Gravity.LEFT);
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}


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

@Override
protected void onStart() {
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,findViewById(R.id.menu_navigation));
    super.onStart();
}

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

}

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

}
// Private class for gestures
private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe 
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    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 {
            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) {
                ModifierBranche.this.onLeftSwipe();

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



}

1 个答案:

答案 0 :(得分:3)

首先按如下方式制作课程

public class OnSwipeTouchListener implements View.OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                    result = true;
                }
            }
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    onSwipeBottom();
                } else {
                    onSwipeTop();
                }
                result = true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
 }
}

然后在你的活动中。只需将侦听器添加到要侦听手势事件的布局或对象。在我的情况下,我已经在我的活动的relativeLayout主要布局上使用它

relativeLayout = (RelativeLayout) findViewById(R.id.content_main);
    relativeLayout.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
        public void onSwipeTop() {
            Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
        }
        public void onSwipeRight() {
            Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
        }
        public void onSwipeLeft() {
            Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
        }
        public void onSwipeBottom() {
            Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
        }

    });

它的工作:)