gpt4 book ai didi

Android长按滚动

转载 作者:行者123 更新时间:2023-12-01 03:42:57 34 4
gpt4 key购买 nike

我想将长按与滚动“连接”起来,这样用户就不必释放屏幕并开始滚动。

我已经实现了手势检测器...

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
// action 1
}

public boolean onScroll(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
// action 2
}
}

public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}

但是现在在操作 1 和操作 2 之间,用户必须释放屏幕...我如何在不释放屏幕的情况下连接这些操作?

最佳答案

我不认为 GestureDetector 会做你想做的事,更有可能你必须自己做。我不知道您当前的设置,下面是一个将 OnToucListener 绑定(bind)到 ScrollView 的类,它将考虑这两个事件:

public class ScrollTouchTest extends Activity {

private final int LONG_PRESS_TIMEOUT = ViewConfiguration
.getLongPressTimeout();
private Handler mHandler = new Handler();
private boolean mIsLongPress = false;
private Runnable longPress = new Runnable() {

@Override
public void run() {
if (mIsLongPress) {
actionOne();
mIsLongPress = false;
}
}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.views_scrolltouchtest);
findViewById(R.id.scrollView1).setOnTouchListener(
new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mIsLongPress = true;
mHandler.postDelayed(longPress, LONG_PRESS_TIMEOUT);
break;
case MotionEvent.ACTION_MOVE:
actionTwo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mIsLongPress = false;
mHandler.removeCallbacks(longPress);
break;
}
return false;
}
});
}

private void actionOne() {
Log.e("XXX", "Long press!!!");
}

private void actionTwo(float f, float g) {
Log.e("XXX", "Scrolling for X : " + f + " Y : " + g);
}

}

关于Android长按滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093232/

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