子控件調用 requestDisallowInterceptTouchEvent(true) 無效 #906

 

 

 

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

子控件調用 requestDisallowInterceptTouchEvent(true) 無效#906

chiemy opened this issue on 16 May 2019 · 5 comments
 

Comments

@chiemy
 
 

chiemy commented on 16 May 2019 • 

edited 

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

 
 
@chiemy chiemy changed the title 子控件調用 requestDisallowInterceptTouchEvent(true) 還是會被攔截 子控件調用 requestDisallowInterceptTouchEvent(true) 無效 on 16 May 2019
@scwang90
 
Owner

scwang90 commented on 25 May 2019

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

@hoop208
 
 

hoop208 commented on 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;
    }

}
 
@WoKee
 
 

WoKee commented on 25 May 2020

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

 
@scwang90
 
Owner

scwang90 commented on 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>
 
@scwang90 scwang90 closed this on 27 May 2020
 
@WoKee
 
 

WoKee commented on 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章