【发布时间】:2017-10-04 12:47:18
【问题描述】:
我有很多孩子的看法。我需要的是对 Swipe 或 Fling 动作做出反应。问题是它只有在我删除所有 Childs 时才真正起作用,否则主布局顶部的子视图会阻止我尝试滑动。
我尝试将 onSwipeListener 添加到主布局并将 GestureListener 添加到整个 Activity 都同样成功。
我当前(无效)的解决方案如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
main_layout = findViewById(R.id.schedule_main_view);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade);
main_layout.startAnimation(fadeInAnimation);
GestureDetector.SimpleOnGestureListener simpleOnGestureListener =
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent event) {
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(null,"Fling");
int dx = (int) (event2.getX() - event1.getX());
// don't accept the fling if it's too short
// as it may conflict with a button push
if (Math.abs(dx) > 20
&& Math.abs(velocityX) > Math.abs(velocityY)) {
if (velocityX > 0) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString());
Log.d(DEBUG_TAG, "onFling To Right");
} else {
Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString());
Log.d(DEBUG_TAG, "onFling To Left");
}
return true;
} else {
return false;
}
}
};
shift = getIntent().getIntExtra(WEEK_SHIFT, CURRENT_WEEK);
mDetector = new GestureDetectorCompat(this,simpleOnGestureListener);
unDimScreen();
setupWeek();
}
重复一遍:如果 Activity 处于顶部没有子视图的状态,则它按预期工作。
所以问题是:我可以做些什么来让活动获取手势忽略覆盖视图?
【问题讨论】:
标签: android swipe gesture-recognition onfling