Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

子控件调用 requestDisallowInterceptTouchEvent(true) 无效 #906

Closed
chiemy opened this issue May 16, 2019 · 5 comments
Closed

子控件调用 requestDisallowInterceptTouchEvent(true) 无效 #906

chiemy opened this issue May 16, 2019 · 5 comments

Comments

@chiemy
Copy link

chiemy commented May 16, 2019

自定义了一个控件,在 onTouchEvent 的 ACTION_DOWN 事件里调用 requestDisallowInterceptTouchEvent(true),事件还是被拦截了,使用官方 SwipeRefreshLayout 则没有这个问题

@chiemy chiemy changed the title 子控件调用 requestDisallowInterceptTouchEvent(true) 还是会被拦截 子控件调用 requestDisallowInterceptTouchEvent(true) 无效 May 16, 2019
@scwang90
Copy link
Owner

能提供一下你的应用场景吗?给一个demo 代码,可以更高更快的解决这个问题。

@hoop208
Copy link

hoop208 commented Aug 14, 2019

有这样一个场景:SmartRefreshLayout里面的内容需要支持长按后绘制当前手指移动的坐标(比如股票的分时图折线图).调用requestDisallowInterceptTouchEvent(true)只是设置了mGroupFlags的标记位.SmartRefreshLayout在重写dispatchTouchEvent的时候好像没有对mGroupFlags |= FLAG_DISALLOW_INTERCEPT的情况做处理.

布局文件:

<TextView
    android:layout_width="match_parent"
    android:text="需求是内容控件长按后显示当前手指移动的坐标,比如股票分时k线图"
    android:layout_height="wrap_content" />

<com.scwang.smartrefresh.layout.SmartRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.scwang.refreshlayout.activity.MoveView
        android:layout_width="match_parent"
        android:background="#33000000"
        android:layout_height="match_parent" />

</com.scwang.smartrefresh.layout.SmartRefreshLayout>

长按后需要绘制坐标的控件:

public class MoveView extends AppCompatTextView {


    //移动的阈值
    private static final int TOUCH_SLOP = 20;
    /**
     * 点击按下事件 X坐标记录
     */
    private float mLastMotionX;
    /**
     * 点击按下事件 Y坐标记录
     */
    private float mLastMotionY;

    private boolean mDelay;

    /**
     * 长按模式的标记位
     */
    private boolean isLongPress;

    /**
     * 长按的runnable
     */
    private Runnable mLongPressRunnable = new Runnable() {
        @Override
        public void run() {
            isLongPress = true;
            mDelay = false;
            getParent().requestDisallowInterceptTouchEvent(true);
        }
    };

    public MoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastMotionX = x;
                mLastMotionY = y;
                postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
                mDelay = true;
                break;
            case MotionEvent.ACTION_MOVE:
                if (isLongPress) {
                    //长按状态下.绘制时间和轴线
                    setText("x=" + x + ";y=" + y);
                } else if (mDelay && (Math.abs(mLastMotionX - x) > TOUCH_SLOP
                        || Math.abs(mLastMotionY - y) > TOUCH_SLOP)) {
                    //移动超过阈值,则表示移动了
                    removeCallbacks(mLongPressRunnable);
                    mDelay = false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            default:
                //释放了
                removeCallbacks(mLongPressRunnable);
                isLongPress = false;
                mDelay = false;
                invalidate();
                break;
        }
        return true;
    }

}

@WoKee
Copy link

WoKee commented May 25, 2020

if (mSuperDispatchTouchEvent) {//如果父类拦截了事件,发送一个取消事件通知
e.setAction(MotionEvent.ACTION_CANCEL);
super.dispatchTouchEvent(e);
}
应该是直接发了一个取消事件通知导致的

@scwang90
Copy link
Owner

添加 android:nestedScrollingEnabled="true" 即可

<com.scwang.smartrefresh.layout.SmartRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.scwang.refreshlayout.activity.MoveView
        android:layout_width="match_parent"
        android:background="#33000000"
        android:layout_height="match_parent" 
        android:nestedScrollingEnabled="true"/>

</com.scwang.smartrefresh.layout.SmartRefreshLayout>

@WoKee
Copy link

WoKee commented May 27, 2020

<ViewFlipper
android:id="@+id/vf_top_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:flipInterval="7000"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:nestedScrollingEnabled="true"
android:background="@drawable/find_viewflipper_bg"
android:inAnimation="@anim/push_down_in"
android:outAnimation="@anim/push_down_out" />

recyclerview 使用 BaseQuickAdapter.addFooterView(ViewFlipper) ;
android:nestedScrollingEnabled无效,当数据不满一页是,触摸ViewFlipper滚动,会触发下拉刷新

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants