在Gallery下,裏面內容過長超出屏幕,這時咱們能夠用ScrollView來滾動,可是這樣作了之後,會發現一個問題,Gallery的滑動事件和ScrollView的滑動事件起衝突,這時咱們能夠自定義Gallery控件來避免這個問題: java
import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.Gallery; public class TestGallery extends Gallery { private float gTouchStartX; private float gTouchStartY; public TestGallery(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public TestGallery(Context context, AttributeSet attrs) { super(context, attrs); } public TestGallery(Context context) { super(context); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch(action){ case MotionEvent.ACTION_DOWN: gTouchStartX = ev.getX(); gTouchStartY = ev.getY(); super.onTouchEvent(ev); break; case MotionEvent.ACTION_MOVE: final float touchDistancesX = Math.abs(ev.getX()-gTouchStartX); final float touchDistancesY = Math.abs(ev.getY()-gTouchStartY); if(touchDistancesY *2 >= touchDistancesX){ return false; }else{ return true; } case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_UP: break; } return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if(e2.getX() > e1.getX()){ onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null); }else{ onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null); } return false; } }