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

以上是我用滚动视图显示照片的代码。
但是如果我想关注下一个,我必须将手指在屏幕上移动更远的距离。
我想让它在移动较小距离时改变焦点
我该怎么做?
我修改了类库如下:

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

但在表现上也有些不正常。

4

1 回答 1

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);
    }
}
于 2012-05-22T08:09:47.540 回答