ViewFlipper 不接收触摸事件
我正在开发一个 Android 项目,其中使用 ViewFlipper 来翻转子项。为此,我使用了定制的脚蹼。在此我重写了 onTouchEvent()
方法。 鳍状肢包含每个子项中的图像,但当我尝试翻转它时,它没有收到任何触摸事件。当我重写 ViewGroup
方法 onInterceptTouchEvent()
并返回 true
时,翻转完成,但由于这个我的 onClickEvent()<未收到每个图像上的 /code>。
我不明白问题出在哪里。当我从 onInterceptTouchEvent()
返回 false
时,将收到点击事件,但不会收到触摸事件。
我缺少什么?
ViewFlipper 的代码:
public class MyViewFlipper extends ViewFlipper {
static final String logTag = "ViewFlipper";
static final int MIN_DISTANCE = 30;
private float downX, downY, upX, upY;
Animation slideLeftIn;
Animation slideLeftOut;
Animation slideRightIn;
Animation slideRightOut;
Context context;
ViewFlipper viewFlipper;
public MyViewFlipper(Context context) {
super(context);
viewFlipper=this;
this.context=context;
System.out.println("I am in MyFlipper() counstructor...");
}
public MyViewFlipper(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
viewFlipper=this;
System.out.println("I am in MyFlipper() counstructor...");
slideLeftIn =
AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
slideLeftOut =
AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
slideRightIn =
AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
slideRightOut =
AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
this.onLeftToRightSwipe();
return true;
}
if (deltaX > 0) {
this.onRightToLeftSwipe();
return true;
}
} else {
if(Math.abs(deltaX)<15){
onClickEvent();
}
//Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
//+ " long, need at least " + MIN_DISTANCE);
}
// swipe vertical?
if (Math.abs(deltaY) > MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
this.onTopToBottomSwipe();
return true;
}
if (deltaY > 0) {
this.onBottomToTopSwipe();
return true;
}
} else {
Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
+ " long, need at least " + MIN_DISTANCE);
}
return true;
}
}
return false;
}
public void onRightToLeftSwipe() {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
}
public void onLeftToRightSwipe() {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
public void onTopToBottomSwipe() {
Log.i(logTag, "onTopToBottomSwipe!");
// activity.doSomething();
}
public void onBottomToTopSwipe() {
Log.i(logTag, "onBottomToTopSwipe!");
// activity.doSomething();
}
public void onClickEvent(){
Toast.makeText(context, "Click",Toast.LENGTH_SHORT);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true; // Here if true then Flipping done.
// And if false then click event done.
}
}
I am working on an Android project in which I use a ViewFlipper
for flipping childs. For this I used a customized flipper. In this I override the onTouchEvent()
method.
The flipper contain images in each child but when I try to flip it doesn't receive any touch event. When I override the ViewGroup
method onInterceptTouchEvent()
and return true
, then flipping is done but due to this my onClickEvent()
on each image is not received.
I am not getting where the problem is. When I return false
from onInterceptTouchEvent()
then the click event is being received but not the touch event.
What am I missing?
Code for the ViewFlipper:
public class MyViewFlipper extends ViewFlipper {
static final String logTag = "ViewFlipper";
static final int MIN_DISTANCE = 30;
private float downX, downY, upX, upY;
Animation slideLeftIn;
Animation slideLeftOut;
Animation slideRightIn;
Animation slideRightOut;
Context context;
ViewFlipper viewFlipper;
public MyViewFlipper(Context context) {
super(context);
viewFlipper=this;
this.context=context;
System.out.println("I am in MyFlipper() counstructor...");
}
public MyViewFlipper(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
viewFlipper=this;
System.out.println("I am in MyFlipper() counstructor...");
slideLeftIn =
AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
slideLeftOut =
AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
slideRightIn =
AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
slideRightOut =
AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
this.onLeftToRightSwipe();
return true;
}
if (deltaX > 0) {
this.onRightToLeftSwipe();
return true;
}
} else {
if(Math.abs(deltaX)<15){
onClickEvent();
}
//Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
//+ " long, need at least " + MIN_DISTANCE);
}
// swipe vertical?
if (Math.abs(deltaY) > MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
this.onTopToBottomSwipe();
return true;
}
if (deltaY > 0) {
this.onBottomToTopSwipe();
return true;
}
} else {
Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
+ " long, need at least " + MIN_DISTANCE);
}
return true;
}
}
return false;
}
public void onRightToLeftSwipe() {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
}
public void onLeftToRightSwipe() {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
public void onTopToBottomSwipe() {
Log.i(logTag, "onTopToBottomSwipe!");
// activity.doSomething();
}
public void onBottomToTopSwipe() {
Log.i(logTag, "onBottomToTopSwipe!");
// activity.doSomething();
}
public void onClickEvent(){
Toast.makeText(context, "Click",Toast.LENGTH_SHORT);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true; // Here if true then Flipping done.
// And if false then click event done.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有类似的问题。
这就是我所做的。我的视图翻转器中有一个列表视图,我重写了列表视图的 setOnTouchListener() 并从那里对视图翻转器进行了动画处理。这对我有用。
尝试在您的情况下覆盖
ImageView
的setOnTouchListener()
。I had a similar problem.
Here is what i did. I had a listview in my view flipper and i override the
setOnTouchListener()
of my listview and animated the view flipper from there. It worked for me.Try overriding the
setOnTouchListener()
ofImageView
in your case.