在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义Gallery控件来避免这个问题:
下面贴出代码:
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;
} }