找到了解决方法。这很可怕,但它有效。
首先,画廊的代码负责我的问题的部分是这样的:
public boolean onDown(MotionEvent e) {
// Kill any existing fling/scroll
mFlingRunnable.stop(false);
// Get the item's view that was touched
mDownTouchPosition = pointToPosition((int) e.getX(), (int) e.getY());
if (mDownTouchPosition >= 0) {
mDownTouchView = getChildAt(mDownTouchPosition - mFirstPosition);
mDownTouchView.setPressed(true);
}
// Reset the multiple-scroll tracking state
mIsFirstScroll = true;
// Must return true to get matching events for this down event.
return true;
}
更确切地说,这一行:
mDownTouchView.setPressed(true);
在这里,我的幻灯片的布局被按下,LinearLayout 的 setPressed 的默认行为是将其分派给所有孩子,因此所有孩子都被按下。
首先,我尝试创建 Gallery 的子类并覆盖 onDown。如果我只是返回 false 而没有其他任何内容,它会起作用,但是幻灯片会在触摸时出现跳到下一张幻灯片的奇怪行为。那是因为这条线:
mFlingRunnable.stop(false);
哪个没有被执行。由于这个变量是私有的并且与 Gallery 类中的所有其他内容相关,我没有找到从子类中使用它的方法。我也尝试复制所有画廊代码,但它也没有工作,因为它使用了很多只有包访问权限的东西......等等。
所以我创建了一个LinearLayout的子类,它覆盖了onSetPressed:
public class LinearLayoutOnSetPressedDoNothing extends LinearLayout {
public LinearLayoutOnSetPressedDoNothing(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setPressed(boolean pressed) {
}
}
并在我的布局中使用它而不是 LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<com.test.LinearLayoutOnPressDoNothing
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- content -->
</com.test.LinearLayoutOnPressDoNothing>
好吧,这行得通。