最近做項目的時候碰到一個這樣的需求:
1. 左右全屏滑動。不需要快速滑動
2. 當Item滑動結束時候需要處理一些業務邏輯。
針對1. 重寫Gallery 的onFling的方法.
public class mygallery extends Gallery {
public mygallery(Context ctx, AttributeSet attrSet) {
super(ctx);
// TODO Auto-generated constructor stub
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, floatvelocityX, float velocityY){
int kEvent;
if(isScrollingLeft(e1, e2)){ //Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else{ //Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
}
針對2. gallery 偵聽Item被選擇時候的事件。
setCallbackDuringFling(false)
setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener).