发布于 2016-08-06 23:48:17 | 850 次阅读 | 评论: 1 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这里我们主要针对ViewGroup的SwipeRefreshLayout中引入ViewPager所引起的滑动冲突问题进行讨论,一起来看一下Android中ViewPager带来的滑动卡顿问题解决要点解析:

问题说明:
当SwipeRefreshLayout中放置了ViewPager控件,两者的滑动会相互冲突.具体表现为ViewPager的左右滑动不顺畅,容易被SwipeRefreshLayout拦截(即出现刷新的View).

问题原因:
ViewPager本身是处理了滚动事件的冲突,它在横向滑动时会调用requestDisallowInterceptTouchEvent()方法使父控件不拦截当前的Touch事件序列.但是SwipeRefreshLayout的requestDisallowInterceptTouchEvent()方法什么也没有做,所以仍然会拦截当前的Touch事件序列.

问题分析:
为什么SwipeRefreshLayout的requestDisallowInterceptTouchEvent()方法什么都不做?

首先SwipeRefreshLayout继承自ViewGroup.

在requestDisallowInterceptTouchEvent()方法什么都不做的情况下,用户可以从底部下拉刷新一次拉出LoadingView.
如果方法调用ViewGroup的requestDisallowInterceptTouchEvent()方法, 可以解决ViewPager的兼容问题,但是用户在界面底部下拉至头部后,无法继续下拉,需要手指放开一次才能拉出LoadingView.
目标分析:
那么为了更加顺滑地滚动,想要的效果当然是一次性拉出LoadingView.既然ViewPager在左右滑动时才会调用requestDisallowInterceptTouchEvent()方法,那么SwipeRefreshLayout只应该在上下滑动时才拦截Touch事件.

具体逻辑如下:

记录是否调用了requestDisallowInterceptTouchEvent()方法,并且设置为true.
在SwipeRefreshLayout中判断是否是上下滑动.
如果同时满足1,2,则调用super.requestDisallowInterceptTouchEvent(true).
否则调用super.requestDisallowInterceptTouchEvent(false).
注意:因为ViewGroup的requestDisallowInterceptTouchEvent方法设置true后,Touch事件在dispatchTouchEvent()方法中就会被拦截,所以需要在dispatchTouchEvent()方法中判断是否为上下滑动.

实现代码(部分):


//非法按键
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);
}



最新网友评论  共有(1)条评论 发布评论 返回顶部
iapcu 发布于2016-09-25 01:03:39
我的没有用呢
支持(1)  反对(0)  回复

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务