ViewPager nested in ViewPager

后端 未结 3 1038
离开以前
离开以前 2020-12-25 09:47

I\'m really newbie in android and I would appreciate any help for my course work.

I need to do:

1) two ViewPagers (not nested) in one Activity

2) two

3条回答
  •  滥情空心
    2020-12-25 10:41

    I added an OnTouchListener to the interior ViewPager:

    private OnTouchListener mSuppressInterceptListener = new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(
                    event.getAction() == MotionEvent.ACTION_DOWN &&
                    v instanceof ViewGroup
            ) {
                    ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
            }
            return false;
        }
    };
    

    This just detects ACTION_DOWN touch events on the inner ViewPager and prevents the outer one from intercepting it. Because it returns false, only the ACTION_DOWN event should be hit; all the other events will be ignored. You can add this listener to every element you want to "protect" from the outer ViewPager's scrolling, though obviously if you want to pick up any other touch behaviour on those elements you'll need to deal with them inside the touch listener and possibly implement a better listener.

提交回复
热议问题