gpt4 book ai didi

java - 如何添加向右滑动打开抽屉导航

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:02 27 4
gpt4 key购买 nike

我有标准的抽屉导航,要打开抽屉导航,您需要从左侧窗口向右滑动。我在不同的手机上尝试过这个,有些手机上有外壳,你不能真正向右滑动(相当困难)。所以我想添加一个滑动手势。因此,当您向右滑动时,抽屉就会打开。我找不到要刷的代码!我试着看,但一切似乎都很模糊。

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

当我滑动时, toast 可以工作,但我尝试将 opendrawer 和 Closerdrawer 添加到左右滑动,但什么也没发生..

最佳答案

更改方法

public void onSwipeRight() {
}

public abstract void onSwipeRight();

...

并使你的类也抽象

public abstract class OnSwipeTouchListener

关于java - 如何添加向右滑动打开抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40595340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com