1

我正在创建一个 Viewflipper。但是,当我跑步并尝试在屏幕上移动我的手时,什么也没有发生。我错过了什么?谢谢

这是java代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);

    flipper = (ViewFlipper)findViewById(R.id.flipperGallery);
    flipper.setOnTouchListener((android.view.View.OnTouchListener) this);
}

@Override
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction())
{
    case MotionEvent.ACTION_DOWN:
    {
    lastX = touchevent.getX();
    Toast.makeText(this, "X: " + lastX, Toast.LENGTH_LONG).show();

    break;
    }
case MotionEvent.ACTION_UP:
    {
    float currentX = touchevent.getX();
    if (lastX < currentX){
        Toast.makeText(this, "scroll right: ", Toast.LENGTH_LONG).show();

        if (flipper.getDisplayedChild()==0) break;
        flipper.setInAnimation(this, R.anim.in_from_left);
        flipper.setOutAnimation(this, R.anim.out_to_right);
        flipper.showNext();
        }
    if (lastX > currentX){
        Toast.makeText(this, "scroll left: ", Toast.LENGTH_LONG).show();

        if (flipper.getDisplayedChild()==1) break;
        flipper.setInAnimation(this, R.anim.in_from_right);
        flipper.setOutAnimation(R.anim.out_to_left);
        flipper.showPrevious();
        }
    break;
    }
}
return false;
}

我正在创建一个 Viewflipper。但是,当我跑步并尝试在屏幕上移动我的手时,什么也没有发生。我错过了什么?谢谢

4

1 回答 1

1

当您在 onTouchEvent 中收到 ACTION_DOWN 并返回 false 时,您不会收到任何进一步的事件,例如 ACTION_UP,...
试试这个:

case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
Toast.makeText(this, "X: " + lastX, Toast.LENGTH_LONG).show();

return true;
}
于 2013-02-13T18:40:47.110 回答