Pass touch event from NestedScrollView to parent view's

5.2k views Asked by At

I have a ViewPager below a NestedScrollView width some top padding, and clipToPadding(false) and transparent background (like as image).

My ViewPager can't get touch event and doesn't work.

How can I solve this problem?

(I can't change my structure and can't move ViewPager to above of NestedScrollView or set TopMargin to NestedScrollView)

ViewPager below a transparent NestedScrollView

ViewPager below a transparent NestedScrollView

NestedScrollView

nestedScrollView = new NestedScrollView(getContext());
nestedScrollView.setFillViewport(true);
nestedScrollView.setLayoutParams(scrollParams);
nestedScrollView.setClipToPadding(false);

Solution:

This Problem solved With overwriting NestedScrollView and Override onTouchEvent. (Thanks to @petrumo)

public class MyNestedScrollView extends NestedScrollView {
    private boolean topZone = false;

    public MyNestedScrollView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN ){
            topZone = (getPaddingTop() - getScrollY() >  ev.getY());
        }

        if(topZone){
            if(ev.getAction() == MotionEvent.ACTION_UP){
                topZone = false;
            }
            return false;
        }
        return super.onTouchEvent(ev);
    }

}
1

There are 1 answers

0
petrumo On BEST ANSWER

There is a workaround for this case, you can override onInterceptTouchEvent and onTouchEvent in the nestedscrollview. There are posts explaining how to do it, https://developer.android.com/training/gestures/viewgroup.html and http://neevek.net/posts/2013/10/13/implementing-onInterceptTouchEvent-and-onTouchEvent-for-ViewGroup.html. When you intercept the event, based on the position and your custom logic you would decide to not use the touch to leave it for the viewpager or let the default scrollview logic handle it.

I am not in favor of this solution, but as you explained you need to have the NestedScrollview cover the viewPager, unless you can reconsider the restrictions