我正在制作自定义的ViewFlipper
,其中我通过覆盖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;
}
}
答案 0 :(得分:0)
删除
onInterceptTouchEvent()
在自定义viewFlipper中,您将返回true
,这意味着,儿童不会获得触摸事件。