gpt4 book ai didi

android - 使用 onInterceptTouchEvent() 时检测触摸事件

转载 作者:行者123 更新时间:2023-11-29 00:27:54 31 4
gpt4 key购买 nike

我问这个相当复杂的问题,希望能从您那里得到一些想法。

事情是这样的:我创建了一个自定义 ListView 类,上面那个:

public class FolderListView extends ListView {

private float xDistance, yDistance, lastX, lastY;
private int folder_index;
private FolderViewInterface openFolder;

// private boolean swiping = false;

// If built programmatically
public FolderListView(Context context) {
super(context);
}

// This example uses this method since being built from XML
public FolderListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

// Build from XML layout
public FolderListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_UP:
// if (swiping == false) {

//if (xDistance > yDistance)
// return false;
// else {
folder_index = pointToPosition((int) lastX, (int) lastY);
// Toast.makeText(getContext(),
// "You pressed a folder with index: " + folder_index,
// Toast.LENGTH_SHORT).show();
// }
HomeScreenFragment f = new HomeScreenFragment();
openFolder = (FolderViewInterface) f;
openFolder.onFolderOpened(folder_index);
// }
// swiping = false;
break;

case MotionEvent.ACTION_DOWN:
// swiping = false;
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
// swiping = true;
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;
if (xDistance > yDistance)
return false;
}

return super.onInterceptTouchEvent(ev);

}

}

我正在寻找正确检测触摸事件的方法。我想通过触摸打开另一个 fragment ,但现在 fragment 是通过触摸和滑动打开的。

我使用了 onInterceptTouchEvent,因为我的列表中的元素是 viewpager,它们在滚动时表现不正常。

我需要某种逻辑来检测滑动而不执行这部分:

HomeScreenFragment f = new HomeScreenFragment();
openFolder = (FolderViewInterface) f;
openFolder.onFolderOpened(folder_index);

仅当事件是触摸事件而不是滑动事件时。

非常感谢任何类型的建议/帮助,如果您需要更多详细信息,请告诉我。

谢谢!

最佳答案

我根据 PRESSED_STATE_DURATION 花费 时间 125

试试这个

//Declare as instance variables
int oldX,oldY,newX,newY;
//State the minimum distance of swipe that you forgive the user for ;)
int distance = 10;
int time = 125;
boolean cancel = false;
case MotionEvent.ACTION_DOWN:
cancel = false;
//Get x and y of touch
oldX = ev.getX();
oldY = ev.getY();
startActionTimer();

case MotionEvent.ACTION_MOVE:
//Get x and y of touch
newX = ev.getX();
newY = ev.getY();

case MotionEvent.ACTION_UP:
cancel = true;

private void startActionTimer(){
//time is pressed state duration
new CountDownTimer(time, time) {
public void onTick(long millisUntilFinished) {}

public void onFinish() {
//Time to do our action!!!
// please consider
if((Math.abs(newX - oldX )< distance) && (Math.abs(newY - oldY ) <distance)){
if(cancel == false){
// Now its a touch so do your action
}
}
}
}.start();
}

注意:请使用所有变量作为类变量(实例变量)以避免出现问题。因为有时我会遇到值未正确更新的奇怪情况

你为什么不试试 gesture detection

并覆盖onSingleTapUp方法

关于android - 使用 onInterceptTouchEvent() 时检测触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18057992/

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