重写 gallery 防止滚过头

package mobi.game.tool.view;

import android.content.Context;

import android.util.AttributeSet;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.widget.Gallery;

/**

 * 重写Gallery,捕捉xy坐标,使其不会跑过头

 * **/

public class DetialGallery extends Gallery {

public DetialGallery(Context context ,AttributeSet attrSet) {

super(context,attrSet);

}

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {

int kEvent;  

if(isScrollingLeft(e1, e2)){ 

kEvent = KeyEvent.KEYCODE_DPAD_LEFT;  

else{ 

kEvent = KeyEvent.KEYCODE_DPAD_RIGHT; 

}  

return onKeyDown(kEvent, null);  

}

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2)

{   

return e2.getX() > e1.getX(); 

}

}

你可能感兴趣的:(gallery)