我想通了;我想为下一个绊倒在这个问题上的人留下一个答案是公平的。我事先向Java纯粹主义者道歉,我写了像C#这样的Java。我也很喜欢在我去的时候搞清楚这个Android的东西。
解决方案是创建一个自定义ListView。其中的神奇之处在于它可以选择是否打开和关闭触摸事件分派(特别是ACTION_MOVE,它是滚动的)。代码如下:
public class UnScrollingListView extends ListView {
public UnScrollingListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
public UnScrollingListView(Context context, AttributeSet attrs) { super(context, attrs); }
public UnScrollingListView(Context context) { super(context); }
public boolean DisableTouchEventScroll = false;
protected boolean DispatchMoveEventInsteadOfConsumingIt = false;
protected View DispatchTarget = null;
public void ResetToNormalListViewBehavior() {
DisableTouchEventScroll = false;
DispatchMoveEventInsteadOfConsumingIt = false;
DispatchTarget = null;
}
public void SetUpDispatch(View v) {
DisableTouchEventScroll = true;
DispatchMoveEventInsteadOfConsumingIt = true;
DispatchTarget = v;
}
protected static float[] GetRelativeCoordinates(View innerView, MotionEvent e) {
int[] screenCoords = new int [2]; //not sure if I have to preallocate this or not.
innerView.getLocationOnScreen(screenCoords);
return new float[] {
e.getRawX() - screenCoords[0],
e.getRawY() - screenCoords[1]};
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
if(DispatchMoveEventInsteadOfConsumingIt && DispatchTarget != null) {
//convert coordinate systems
float[] newCoords = GetRelativeCoordinates(DispatchTarget, ev);
ev.setLocation(newCoords[0], newCoords[1]);
DispatchTarget.onTouchEvent(ev);
return true;
}
if(DisableTouchEventScroll && ev.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}
return super.dispatchTouchEvent(ev);
}
}
默认情况下,它的行为与普通列表视图相似。您可以调用SetUpDispatch(View)来告诉它停止滚动列表视图并将所有ACTION_MOVE事件分派给特定的视图。然后,您可以调用ResetToNormalListViewBehavior()使其重新开始滚动。在我原来的职位挂钩的签名捕捉代码,所有你需要做的是改变了的onTouchEvent如下:
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
...
listView.SetUpDispatch(this);
return true;
case MotionEvent.ACTION_UP:
listView.ResetToNormalListViewBehavior();
case MotionEvent.ACTION_MOVE:
...
不知道会不会有人曾经遇到这样的问题,但它似乎有一些一般的应用,如果你读过这个,祝你好运。