我正在创建一个 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。但是,当我运行并尝试在屏幕上移动我的手时,什么也没有发生。我错过了什么? 谢谢
最佳答案
当您在 onTouchEvent 中收到 ACTION_DOWN 并返回 false 时,您不会收到任何进一步的事件,例如 ACTION_UP,...
试试这个:
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
Toast.makeText(this, "X: " + lastX, Toast.LENGTH_LONG).show();
return true;
}
关于java - 取景器不翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14860493/