6

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);
    }

}
Community
  • 1
  • 1
Alireza MH
  • 573
  • 8
  • 25
  • 6
    That should be a new standard of how images look when talking about the structure of a layout. – DeeV Dec 13 '16 at 15:48
  • Anyway, does the Viewpager work when the scrollview is gone? I suspect that the scrollview is intercepting the touch events before it gets to the Viewpager. – DeeV Dec 13 '16 at 15:49
  • I've something like as profile fragment of whatsapp, something like collapsingToolbar – Alireza MH Dec 13 '16 at 15:52
  • @DeeV Yes it works. It also works when i bring the viewpager on top of the scrollview. – Alireza MH Dec 13 '16 at 15:59
  • Is it an option to use the instead of topPadding? – petrumo Dec 13 '16 at 16:30
  • @petrumo Can you explain it more precisely? – Alireza MH Dec 13 '16 at 16:52
  • ... if you can push down the NestedScrollView, by adding a view before it? or you have the same constrains as with the topMargin – petrumo Dec 13 '16 at 17:58
  • @petrumo I need to setFillViewport(true), If I set TopMargin or put a view before It, so I never can cover ViewPager with my content. (To better explain I draw another Image) – Alireza MH Dec 14 '16 at 04:54

1 Answers1

1

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

petrumo
  • 1,116
  • 9
  • 18