Android中ViewPager帶來的滑動卡頓問題解決要點解析
問題說明:
當(dāng)SwipeRefreshLayout中放置了ViewPager控件,兩者的滑動會相互沖突.具體表現(xiàn)為ViewPager的左右滑動不順暢,容易被SwipeRefreshLayout攔截(即出現(xiàn)刷新的View).
問題原因:
ViewPager本身是處理了滾動事件的沖突,它在橫向滑動時會調(diào)用requestDisallowInterceptTouchEvent()方法使父控件不攔截當(dāng)前的Touch事件序列.但是SwipeRefreshLayout的requestDisallowInterceptTouchEvent()方法什么也沒有做,所以仍然會攔截當(dāng)前的Touch事件序列.
問題分析:
為什么SwipeRefreshLayout的requestDisallowInterceptTouchEvent()方法什么都不做?
首先SwipeRefreshLayout繼承自ViewGroup.
在requestDisallowInterceptTouchEvent()方法什么都不做的情況下,用戶可以從底部下拉刷新一次拉出LoadingView.
如果方法調(diào)用ViewGroup的requestDisallowInterceptTouchEvent()方法, 可以解決ViewPager的兼容問題,但是用戶在界面底部下拉至頭部后,無法繼續(xù)下拉,需要手指放開一次才能拉出LoadingView.
目標(biāo)分析:
那么為了更加順滑地滾動,想要的效果當(dāng)然是一次性拉出LoadingView.既然ViewPager在左右滑動時才會調(diào)用requestDisallowInterceptTouchEvent()方法,那么SwipeRefreshLayout只應(yīng)該在上下滑動時才攔截Touch事件.
具體邏輯如下:
記錄是否調(diào)用了requestDisallowInterceptTouchEvent()方法,并且設(shè)置為true.
在SwipeRefreshLayout中判斷是否是上下滑動.
如果同時滿足1,2,則調(diào)用super.requestDisallowInterceptTouchEvent(true).
否則調(diào)用super.requestDisallowInterceptTouchEvent(false).
注意:因為ViewGroup的requestDisallowInterceptTouchEvent方法設(shè)置true后,Touch事件在dispatchTouchEvent()方法中就會被攔截,所以需要在dispatchTouchEvent()方法中判斷是否為上下滑動.
實現(xiàn)代碼(部分):
//非法按鍵 private static final int INVALID_POINTER = -1; //dispatch方法記錄第一次按下的x private float mInitialDisPatchDownX; //dispatch方法記錄第一次按下的y private float mInitialDisPatchDownY; //dispatch方法記錄的手指 private int mActiveDispatchPointerId = INVALID_POINTER; //是否請求攔截 private boolean hasRequestDisallowIntercept = false; @Override public void requestDisallowInterceptTouchEvent(boolean b) { hasRequestDisallowIntercept = b; // Nope. } @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mActiveDispatchPointerId = MotionEventCompat.getPointerId(ev, 0); final float initialDownX = getMotionEventX(ev, mActiveDispatchPointerId); if (initialDownX != INVALID_POINTER) { mInitialDisPatchDownX = initialDownX; } final float initialDownY = getMotionEventY(ev, mActiveDispatchPointerId); if (mInitialDisPatchDownY != INVALID_POINTER) { mInitialDisPatchDownY = initialDownY; } break; case MotionEvent.ACTION_MOVE: if (hasRequestDisallowIntercept) { //解決viewPager滑動沖突問題 final float x = getMotionEventX(ev, mActiveDispatchPointerId); final float y = getMotionEventY(ev, mActiveDispatchPointerId); if (mInitialDisPatchDownX != INVALID_POINTER && x != INVALID_POINTER && mInitialDisPatchDownY != INVALID_POINTER && y != INVALID_POINTER) { final float xDiff = Math.abs(x - mInitialDisPatchDownX); final float yDiff = Math.abs(y - mInitialDisPatchDownY); if (xDiff > mTouchSlop && xDiff * 0.7f > yDiff) { //橫向滾動不需要攔截 super.requestDisallowInterceptTouchEvent(true); } else { super.requestDisallowInterceptTouchEvent(false); } } else { super.requestDisallowInterceptTouchEvent(false); } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) { hasRequestDisallowIntercept = false; } break; } return super.dispatchTouchEvent(ev); } private float getMotionEventY(MotionEvent ev, int activePointerId) { final int index = MotionEventCompat.findPointerIndex(ev, activePointerId); if (index < 0) { return -1; } return MotionEventCompat.getY(ev, index); } private float getMotionEventX(MotionEvent ev, int activePointerId) { final int index = MotionEventCompat.findPointerIndex(ev, activePointerId); if (index < 0) { return -1; } return MotionEventCompat.getX(ev, index); }
- 使用ViewPager實現(xiàn)左右循環(huán)滑動及滑動跳轉(zhuǎn)
- Android App中ViewPager所帶來的滑動沖突問題解決方法
- 自定義RadioButton和ViewPager實現(xiàn)TabHost帶滑動的頁卡效果
- Android App中使用ViewPager+Fragment實現(xiàn)滑動切換效果
- Android利用ViewPager實現(xiàn)滑動廣告板實例源碼
- Android開發(fā)之使用ViewPager實現(xiàn)圖片左右滑動切換效果
- Android ViewPager無限循環(huán)實現(xiàn)底部小圓點動態(tài)滑動
- Android實現(xiàn)橫向滑動卡片效果
- Android仿探探卡片式滑動效果實現(xiàn)
- ViewPager+RadioGroup實現(xiàn)左右滑動卡片布局
相關(guān)文章
項目發(fā)布Debug和Release版的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了項目發(fā)布Debug和Release版的區(qū)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10Android使用AudioManager修改系統(tǒng)音量的方法
這篇文章主要介紹了Android使用AudioManager修改系統(tǒng)音量的方法,結(jié)合實例形式分析了AudioManager調(diào)節(jié)音量的常用方法及相關(guān)使用技巧,需要的朋友可以參考下2016-08-08Android開發(fā)之TabActivity用法實例詳解
這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實例形式較為詳細(xì)的分析了Android擴展Activity實現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-03-03Android開發(fā)實現(xiàn)的IntentUtil跳轉(zhuǎn)多功能工具類【包含視頻、音頻、圖片、攝像頭等操作功能】
這篇文章主要介紹了Android開發(fā)實現(xiàn)的IntentUtil跳轉(zhuǎn)多功能工具類,該封裝類還包含視頻、音頻、圖片、攝像頭等操作功能,需要的朋友可以參考下2017-11-11Kotlin中常見內(nèi)聯(lián)擴展函數(shù)的使用方法教程
在Kotlin中,使用inline修飾符標(biāo)記內(nèi)聯(lián)函數(shù),既會影響到函數(shù)本身, 也影響到傳遞給它的Lambda表達(dá)式,這兩者都會被內(nèi)聯(lián)到調(diào)用處。下面這篇文章主要給大家介紹了關(guān)于Kotlin中常見內(nèi)聯(lián)擴展函數(shù)的使用方法,需要的朋友可以參考下。2017-12-12Android開發(fā)案例手冊Application跳出dialog
這篇文章主要為大家介紹了Android開發(fā)案例手冊Application跳出dialog,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android使用SQLite數(shù)據(jù)庫的簡單實例
這篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫的簡單實例,有需要的朋友可以參考一下2013-12-12Android GSYVideoPlayer視頻播放器功能的實現(xiàn)
這篇文章主要介紹了Android GSYVideoPlayer視頻播放器功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03