本文介绍了将NestedScrollView的touch事件传递给父视图的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个NestedScrollView宽度下方有一个ViewPager,顶部填充了一定的宽度,还有clipToPadding(false)和透明背景(如图像).

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

我的ViewPager无法获得触摸事件,并且不起作用.

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

我该如何解决这个问题?

How can I solve this problem?

(我无法更改结构,也无法将ViewPager移至NestedScrollView的上方或将TopMargin设置为NestedScrollView)

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


解决方案:

此问题通过覆盖NestedScrollView和覆盖onTouchEvent得以解决.(感谢 @petrumo )


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

}

推荐答案

在这种情况下,有一种解决方法,您可以在nestedscrollview中覆盖onInterceptTouchEvent和onTouchEvent.有文章解释了如何执行操作, https://developer.android.com/training/gestures/viewgroup.html http://neevek.net/posts/2013/10/13/implementing-onInterceptTouchEvent-and-onTouchEvent-for-ViewGroup.html .拦截事件时,根据位置和自定义逻辑,您将决定不使用触摸将其留给viewpager或让默认的scrollview逻辑对其进行处理.

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.

我不赞成这种解决方案,但是正如您所解释的,除非您可以重新考虑限制,否则您需要让NestedScrollview覆盖viewPager

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

这篇关于将NestedScrollView的touch事件传递给父视图的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:54