国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android中ViewPager帶來的滑動(dòng)卡頓問題解決要點(diǎn)解析

2019-12-12 06:07:33
字體:
供稿:網(wǎng)友

問題說明:
當(dāng)SwipeRefreshLayout中放置了ViewPager控件,兩者的滑動(dòng)會(huì)相互沖突.具體表現(xiàn)為ViewPager的左右滑動(dòng)不順暢,容易被SwipeRefreshLayout攔截(即出現(xiàn)刷新的View).

問題原因:
ViewPager本身是處理了滾動(dòng)事件的沖突,它在橫向滑動(dòng)時(shí)會(huì)調(diào)用requestDisallowInterceptTouchEvent()方法使父控件不攔截當(dāng)前的Touch事件序列.但是SwipeRefreshLayout的requestDisallowInterceptTouchEvent()方法什么也沒有做,所以仍然會(huì)攔截當(dāng)前的Touch事件序列.

問題分析:
為什么SwipeRefreshLayout的requestDisallowInterceptTouchEvent()方法什么都不做?

首先SwipeRefreshLayout繼承自ViewGroup.

在requestDisallowInterceptTouchEvent()方法什么都不做的情況下,用戶可以從底部下拉刷新一次拉出LoadingView.
如果方法調(diào)用ViewGroup的requestDisallowInterceptTouchEvent()方法, 可以解決ViewPager的兼容問題,但是用戶在界面底部下拉至頭部后,無法繼續(xù)下拉,需要手指放開一次才能拉出LoadingView.
目標(biāo)分析:
那么為了更加順滑地滾動(dòng),想要的效果當(dāng)然是一次性拉出LoadingView.既然ViewPager在左右滑動(dòng)時(shí)才會(huì)調(diào)用requestDisallowInterceptTouchEvent()方法,那么SwipeRefreshLayout只應(yīng)該在上下滑動(dòng)時(shí)才攔截Touch事件.

具體邏輯如下:

記錄是否調(diào)用了requestDisallowInterceptTouchEvent()方法,并且設(shè)置為true.
在SwipeRefreshLayout中判斷是否是上下滑動(dòng).
如果同時(shí)滿足1,2,則調(diào)用super.requestDisallowInterceptTouchEvent(true).
否則調(diào)用super.requestDisallowInterceptTouchEvent(false).
注意:因?yàn)閂iewGroup的requestDisallowInterceptTouchEvent方法設(shè)置true后,Touch事件在dispatchTouchEvent()方法中就會(huì)被攔截,所以需要在dispatchTouchEvent()方法中判斷是否為上下滑動(dòng).

實(shí)現(xiàn)代碼(部分):

//非法按鍵private static final int INVALID_POINTER = -1;//dispatch方法記錄第一次按下的xprivate float mInitialDisPatchDownX;//dispatch方法記錄第一次按下的yprivate float mInitialDisPatchDownY;//dispatch方法記錄的手指private int mActiveDispatchPointerId = INVALID_POINTER;//是否請(qǐng)求攔截private boolean hasRequestDisallowIntercept = false;@Overridepublic void requestDisallowInterceptTouchEvent(boolean b) {  hasRequestDisallowIntercept = b;  // Nope.}@Overridepublic 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滑動(dòng)沖突問題        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) {            //橫向滾動(dòng)不需要攔截            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);}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 河津市| 庄浪县| 驻马店市| 长阳| 汝城县| 华宁县| 崇州市| 鲁山县| 板桥市| 滦南县| 遂溪县| 泰和县| 资兴市| 杭州市| 松滋市| 名山县| 龙山县| 安吉县| 准格尔旗| 克东县| 正宁县| 驻马店市| 军事| 商城县| 英吉沙县| 天等县| 远安县| 金川县| 淅川县| 和硕县| 柘城县| 安吉县| 山西省| 克山县| 平罗县| 壤塘县| 安龙县| 汾西县| 溧水县| 郸城县| 恩平市|