我在问这个相当复杂的问题,希望我能从你那里得到一个想法。
事情是这样的:我创建了一个自定义列表视图类,上面的那个:
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);
}
}
事情就在这里,我正在寻找正确检测触摸事件。我想在触摸时打开另一个片段,但现在该片段在触摸和滑动时都打开了。
我使用了 onInterceptTouchEvent,因为我的列表中的元素是 viewpagers,并且在滚动时它们的行为不正确。
我需要某种逻辑来检测滑动而不执行这部分:
HomeScreenFragment f = new HomeScreenFragment();
openFolder = (FolderViewInterface) f;
openFolder.onFolderOpened(folder_index);
仅当事件是触摸事件而不是滑动事件时。
非常感谢任何形式的建议/帮助,如果您需要更多详细信息,请告诉我。
谢谢!