Pass touch event from NestedScrollView to parent view's

荒凉一梦 提交于 2020-01-02 07:40:31

问题


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)

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 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



来源:https://stackoverflow.com/questions/41124973/pass-touch-event-from-nestedscrollview-to-parent-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!