本来是要做一个上下滑动的布局,但是里面还要嵌套一个可以上下滑动且上拉刷新的布局。如下图:
使用NestedScrollView+RecycleView可以解决滑动冲突的问题,但是上拉刷新会卡顿。
猜想:应该是顶层布局对事件进行了拦截,没有把触摸事件下发。
试验:重写NestedScrollView的拦截事件onInterceptTouchEvent()判断其是否已经拉到最后且判断其是上拉还是下拉。
可能代码不够精致,但是基本功能已实现。
代码如下:
public class PullToFreshScrollView extends NestedScrollView{
private float mLastY = 0;
private float mActionDown = 0;
private float mActionUp = 0;
public PullToFreshScrollView(Context context) {
super(context);
}
public PullToFreshScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
mActionDown = ev.getY();
case MotionEvent.ACTION_UP:
mActionUp = ev.getY();
if(isLast()&&(mActionUp-mActionDown<0)){
return false;
}
break;
case MotionEvent.ACTION_MOVE:
mLastY = ev.getY();
if(isLast()&&(mLastY-mActionDown<0)){
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
private boolean isLast() {
int scrollY = getScrollY();
View onlyChild = getChildAt(0);
if (onlyChild.getHeight() <= scrollY + getHeight()) {
return true;
}
return false;
}
}
为什么重写了这个方法就不冲突了呢,要了解一下View和ViewGroup的touch事件。
View和ViewGroup都有touchEvent和dispatchTouchEvent事件。
touchEvent即消费事件;
dispatchTouchEvent是决定事件是否分发给下一级;
而ViewGroup比View多了一个onInterceptTouchEvent事件,决定分发这个事件后还要决定是否拦截这个分发,返回false表示不拦截,可以向下分发,返回true则表示自身消费了,不往下发。