【发布时间】:2017-04-24 19:57:00
【问题描述】:
所以我有一个活动,我有一个导航抽屉。我取消了滑动以打开该导航抽屉(仅当我单击该菜单的按钮时才会打开)。
现在我想通过滑动来改变活动(就像在 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;
}
}
}
【问题讨论】:
-
它什么也没做...
-
我写的代码和你提供给我的链接完全一样..
-
你有什么想法吗?
-
完全滑动不起作用??
标签: android swipe-gesture