Search code examples
androidandroid-viewpagerfragmentontouch

FrameLayout above a viewPager in fragment the onTouch Event has bean intercepted


a fragment has two views a FrameLayout contains some children views ,below the FrameLayout there has a viewPager . I want to replace the fragment when there has an action like ACTION_MOVE on FrameLayout ,so I add a onTouchListener on FrameLayout but it never work,viewPager works well,and also the FrameLayout's children views has onClick event

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view =  inflater.inflate(R.layout.hall_gift, container,
            false);
    frameLayout = (FrameLayout)view.findViewById(R.id.fans_body_title);
    frameLayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN){
                Log.i(TAG,"ACTION DOWN");
            }

            return false;
        }
    });
    return view;
}

Solution

  • You can extend the FrameLayout and override its onInterceptTouchEvent() to intercept the touch events from child view. Then, use your self-defined FrameLayout instead of the previous FrameLayout.

    Plz refer to this document: http://developer.android.com/training/gestures/viewgroup.html#intercept

    Here is a code snippet:

    public class MyFrameLayout extends FrameLayout {
    
       @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
    
        /*
         * This method determines whether we want to intercept the motion.
         * If we return true, onTouchEvent will be called.
         */
    
               final int action = MotionEventCompat.getActionMasked(ev);    
                switch (action) {
                    case MotionEvent.ACTION_MOVE: {
                        if(meet your condition){
                            return true;
                        }
                    }
    
                }
            return false;
        }
    
    }