实现 Fragment 触摸事件处理

设置触摸事件可以在主activity中设置一个接口如下
private ArrayList onTouchListeners = new ArrayList(
                        10);

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
                for (MyOnTouchListener listener : onTouchListeners) {
                        listener.onTouch(ev);
                }
                return super.dispatchTouchEvent(ev);
        }

        public void registerMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
                onTouchListeners.add(myOnTouchListener);
        }
        public void unregisterMyOnTouchListener(MyOnTouchListener myOnTouchListener) {
                onTouchListeners.remove(myOnTouchListener) ;
        }
        public interface MyOnTouchListener {
                public boolean onTouch(MotionEvent ev);
        }



然后再在fragment中实例化一个mainactivity.myOntouchlistener
myOnTouchListener=new MainActivity.MyOnTouchListener() {
                        
                        @Override
                        public boolean onTouch(MotionEvent ev) {
                                        boolean result = mGestureDetector.onTouchEvent(ev);
                                return result;
                        }
                };
        ((MainActivity)getActivity()).registerMyOnTouchListener(myOnTouchListener);


你可能感兴趣的:(android)