1. 程式人生 > 其它 >子控制元件呼叫 requestDisallowInterceptTouchEvent(true) 無效 #906

子控制元件呼叫 requestDisallowInterceptTouchEvent(true) 無效 #906

Closed chiemyopened this issueon 16 May 2019· 5 comments Closed

子控制元件呼叫 requestDisallowInterceptTouchEvent(true) 無效#906

chiemyopened this issueon 16 May 2019· 5 comments

Comments

chiemycommentedon 16 May 2019

edited

自定義了一個控制元件,在 onTouchEvent 的 ACTION_DOWN 事件裡呼叫 requestDisallowInterceptTouchEvent(true),事件還是被攔截了,使用官方 SwipeRefreshLayout 則沒有這個問題

chiemychanged the title子控制元件呼叫 requestDisallowInterceptTouchEvent(true) 還是會被攔截子控制元件呼叫 requestDisallowInterceptTouchEvent(true) 無效on 16 May 2019 Owner

scwang90commentedon 25 May 2019

能提供一下你的應用場景嗎?給一個demo 程式碼,可以更高更快的解決這個問題。

hoop208commentedon 14 Aug 2019

edited

有這樣一個場景: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;
    }

}

WoKeecommentedon 25 May 2020

if (mSuperDispatchTouchEvent) {//如果父類攔截了事件,傳送一個取消事件通知
e.setAction(MotionEvent.ACTION_CANCEL);
super.dispatchTouchEvent(e);
}
應該是直接發了一個取消事件通知導致的

scwang90added thegood first issuelabelon 27 May 2020 Owner

scwang90commentedon 27 May 2020

新增 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>
scwang90closed thison 27 May 2020

WoKeecommentedon 27 May 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滾動,會觸發下拉重新整理

https://github.com/scwang90/SmartRefreshLayout/issues/906