MotionLayout prevents ClickListener on all Views

后端 未结 3 2105
自闭症患者
自闭症患者 2021-01-21 20:46

I am using a MotionLayout with a scene-xml:


    <         


        
3条回答
  •  萌比男神i
    2021-01-21 21:41

    @muetzenflo's response is the most efficient solution I've seen so far for this problem.

    However, only checking the Event.Action for MotionEvent.ACTION_MOVE causes the MotionLayout to respond poorly. It is better to differentiate between movement and a single click by the use of ViewConfiguration.TapTimeout as the example below demonstrates.

    public class MotionSubLayout extends MotionLayout {
    
        private long mStartTime = 0;
    
        public MotionSubLayout(@NonNull Context context) {
            super(context);
        }
    
        public MotionSubLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MotionSubLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            if ( event.getAction() == MotionEvent.ACTION_DOWN ) {
                mStartTime = event.getEventTime();
            } else if ( event.getAction() == MotionEvent.ACTION_UP ) {
                if ( event.getEventTime() - mStartTime <= ViewConfiguration.getTapTimeout() ) {
                    return false;
                }
            }
    
            return super.onInterceptTouchEvent(event);
        }
    }
    

提交回复
热议问题