簡體   English   中英

如何向右滑動以打開導航抽屜

[英]How to add swipe right to open navigation drawer

我有標准的導航抽屜,打開你需要從左側窗口向右滑動的導航抽屜。 我在不同的手機上試過這個,有些人在它上面有貝殼,你不能真正向右滑動(非常難)。 所以我想添加一個滑動手勢。 因此,當您向右滑動時,抽屜將打開。 我找不到刷卡的代碼! 我試着看,但一切看起來都很模糊。

  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

這是我的一些代碼。 如果您還有其他需要,請告訴我! 希望有人能解決這個問題!..

更新:我找到了滑動代碼並上了課。

 import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class OnSwipeTouchListener implements 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 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() { } } 

這是我在MainActivity類中使用的代碼

  drawer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { } }); drawer.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this ) { public boolean onSwipeTop() { Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show(); return true; } public boolean onSwipeRight() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.openDrawer(GravityCompat.START); return true; } public boolean onSwipeLeft() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } public boolean onSwipeBottom() { Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show(); return true; } }); 

吐司工作時,我刷卡但我嘗試添加opendrawer和closedrawer左右滑動,但沒有任何事情發生..

改變方法

public void onSwipeRight() {
    }

public abstract void onSwipeRight();

...

並使你的班級也抽象化

public abstract class OnSwipeTouchListener

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM