翻转视图翻转时不出现点击事件
我正在开发自定义的ViewFlipper
,其中我通过重写flipper的OnTouch()
来翻转页面。但我的问题是我无法获取添加到脚蹼中的孩子的触摸事件。并且仅在 ViewFlipper
空白部分获取直接触摸事件。那么我如何在启用子点击事件的所有页面上获取触摸事件。这是我的自定义 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;
}
public MyViewFlipper(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
viewFlipper=this;
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;
}
}
I am working on customized ViewFlipper
in which I flip the pages by overriding OnTouch()
of flipper. But my problem is m not able to get touch event on child which are added into flipper. And get direct touch event only on ViewFlipper
empty part. So how I can get touch event on all of page with enabled child click event. Here is my custom ViewFlipper
code
Thank You.
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;
}
public MyViewFlipper(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
viewFlipper=this;
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的自定义 viewFlipper 中删除
,您将返回
true
这意味着,孩子们不会收到触摸事件。Remove
in your custom viewFlipper, you are returning
true
which means , children will not get touch events.