MyGallery gallery = (MyGallery)findViewById(R.id.gallery_photo);
PhonePhotoViewerAdapter = new PhonePhotoViewerAdapter(this, FilePath);
gallery.setAdapter(PhonePhotoViewerAdapter);
gallery.setSelection(0);
以上是我用scrollview显示照片的代码 但如果我想专注于下一步,我必须用更大的距离在屏幕上移动手指 我想让它在更小的距离移动时改变焦点 我该怎么办? 我修改了类库如下:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 50 && Math.abs(velocityX) > 100) {
// Fling left
} else if (e2.getX() - e1.getX() > 50 && Math.abs(velocityX) > 100) {
// Fling right
}
return false;
}
以下代码是我的修改:
gallery.setLongClickable(false);
gallery.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if((action == MotionEvent.ACTION_DOWN) && !Clicking) {
StartX = event.getX();
StartIndex = gallery.getSelectedItemPosition();
Clicking = true;
}
else if((action == MotionEvent.ACTION_UP) && Clicking) {
EndX = event.getX();
EndIndex = gallery.getSelectedItemPosition();
Clicking = false;
if(((EndX - StartX) > 50) && (StartIndex == EndIndex)) {
if(EndIndex > 0) {
gallery.setSelection(EndIndex - 1);
}
}
else if(((StartX - EndX) > 50) && (StartIndex == EndIndex)) {
if(EndIndex < count - 1) {
gallery.setSelection(EndIndex + 1);
}
}
}
return false;
}
});
但是在表现方面存在一些异常。
答案 0 :(得分:1)
我找到了问题的解决方案 这是MyGallery定义如下:
public class MyGallery extends Gallery {
public MyGallery(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)) {
//Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else {
//Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}
@Override
protected android.view.ViewGroup.LayoutParams generateLayoutParams (android.view.ViewGroup.LayoutParams p) {
return super.generateLayoutParams(p);
}
}