简体   繁体   English

如何忽略上层视图并检测onFling(onSwipe)?

[英]How to ignore overlying views and detect onFling (onSwipe)?

I have a view with a lot of Childs. 我对很多孩子都有看法。 What I need is to implement reaction on Swipe or on Fling moves. 我需要的是对Swipe或Fling动作执行反应。 Problem is that it only really works if I remove all Childs, otherwise Child views on the top of the main layout block my attempts to swipe. 问题是,只有删除所有“子项”,它才真正起作用,否则,主布局顶部的“子项”视图会阻止我尝试滑动。

I tried both adding onSwipeListener to the main layout and adding GestureListener to the whole activity with the same success. 我尝试将onSwipeListener添加到主布局中以及将GestureListener添加到整个活动中均获得了相同的成功。

My current (non-working) solution looks like: 我当前(无效)的解决方案如下所示:

 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();
    }

To repeat: if the activity is in the state when there are no child views on top, it works as intended. 重复:如果活动处于顶部没有子视图的状态,则它会按预期工作。

So the question is: what I can do to make activity fetch gestures ignoring the overlying views? 所以问题是:我该怎么做才能使活动获取手势而忽略上层视图?

The problem is child views getting touch events and not giving it to the parent. 问题在于子视图会获得触摸事件,而不是将其提供给父视图。 If you are not using on overlying views clickable events, you can turn off on that views clickable property like view.setClickable(false); 如果不使用覆盖视图的可点击事件,则可以关闭该视图的可点击属性,例如view.setClickable(false); ... Then all click events will go it's parent view. ...然后所有点击事件将进入其父视图。 If it doesn't works, you can define on touch listener on every overlying views like this: 如果它不起作用,则可以在每个重叠的视图上定义触摸监听器,如下所示:

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }
});

UPD: Here another (right) solution of this problem: https://developer.android.com/training/gestures/viewgroup.html#delegate UPD:以下是此问题的另一个(正确)解决方案: https : //developer.android.com/training/gestures/viewgroup.html#delegate

Try setting android:clickable="true" and android:descendantFocusability="blocksDescendants" to the view you want to swipe in the xml file. 尝试将android:clickable="true"android:descendantFocusability="blocksDescendants"设置为要在xml文件中滑动的视图。 This should block children from receiving click events. 这应该阻止孩子接收点击事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM