在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;
}
}