Tuesday, May 22, 2012

How to change gallery's scrollview

MyGallery gallery = (MyGallery)findViewById(R.id.gallery_photo);
PhonePhotoViewerAdapter = new PhonePhotoViewerAdapter(this, FilePath);
gallery.setAdapter(PhonePhotoViewerAdapter);
gallery.setSelection(0);


Above is my code to show photo with scrollview.

But if I want to focus on next, I have to move finger on screen with a larger distance.

I want to let it change focus while a smaller distance moved

How can I do it?

I had modify class Gallery as below:



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


Below code is my modify:



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


But there are some abnormal in showing.





No comments:

Post a Comment