1、简介
想必大家在开发中不可避免的都在使用RecyclerView吧,那么也应该在NestScrollView中嵌套过RecyclerView吧,但是呢,你会发现当你的Rv和Nsv同向的时候,那么会遇到滑动失去惯性的问题,这就是我们的问题一,再有当我们的Nsv是固定高度的,那么会带来Rv的滑动问题的,这是问题二,以下我们就探讨下这两个问题。
2、滑动惯性解决方案
说来惭愧,我也不知道是啥问题,下面先贴出解决方案,后面知道了再进行补充吧
manager.setSmoothScrollbarEnabled(true);
manager.setAutoMeasureEnabled(true);
mRvContent.setHasFixedSize(true);
mRvContent.setNestedScrollingEnabled(false);
3. 解决限定高度的滑动冲突
3.1情景还原
布局如下图所示,整个布局的主体是个滑动NestScrollView,上部分为内容的主体,固定样式,而下部分则为在固定高度的LinearLayout中的RecyclerVierw。然后怎么滑动都没有反应
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mydairytestproject.ConflictActivity">
<com.example.mydairytestproject.MyNestedView
android:id="@+id/mNestView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="500dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="420dp">
<com.example.mydairytestproject.NoConflictRecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.example.mydairytestproject.NoConflictRecyclerView>
</RelativeLayout>
</LinearLayout>
</com.example.mydairytestproject.MyNestedView>
</LinearLayout>
问题解释
首先应该清楚的是,NewstScrollview的滑动原理是通过 scrollTo()来调整并记录它的滑动位置,而且只要是有拖拽的动作那么NesredScrollView就会拦截掉滑动的 事件,由它自己去处理滑动,即滚动控件。所以我们需要处理的是当RecyclerView滑动的时候,对应的父布局 NestScrollVew不允许拦截。
下面贴出NestScrollView的源码
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
/*
* This method JUST determines whether we want to intercept the motion.
* If we return true, onMotionEvent will be called and we do the actual
* scrolling there.
*/
/*
* Shortcut the most recurring case: the user is in the dragging
* state and he is moving his finger. We want to intercept this
* motion.
*/
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
return true;
}
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_MOVE: {
/*
* mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
* whether the user has moved far enough from his original down touch.
*/
/*
* Locally do absolute value. mLastMotionY is set to the y value
* of the down event.
*/
final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER) {
// If we don't have a valid id, the touch down wasn't on content.
break;
}
final int pointerIndex = ev.findPointerIndex(activePointerId);
if (pointerIndex == -1) {
Log.e(TAG, "Invalid pointerId=" + activePointerId
+ " in onInterceptTouchEvent");
break;
}
final int y = (int) ev.getY(pointerIndex);
final int yDiff = Math.abs(y - mLastMotionY);
if (yDiff > mTouchSlop
&& (getNestedScrollAxes() & ViewCompat.SCROLL_AXIS_VERTICAL) == 0) {
mIsBeingDragged = true;
mLastMotionY = y;
initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(ev);
mNestedYOffset = 0;
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
break;
}
case MotionEvent.ACTION_DOWN: {
final int y = (int) ev.getY();
if (!inChild((int) ev.getX(), y)) {
mIsBeingDragged = false;
recycleVelocityTracker();
break;
}
/*
* Remember location of down touch.
* ACTION_DOWN always refers to pointer index 0.
*/
mLastMotionY = y;
mActivePointerId = ev.getPointerId(0);
initOrResetVelocityTracker();
mVelocityTracker.addMovement(ev);
/*
* If being flinged and user touches the screen, initiate drag;
* otherwise don't. mScroller.isFinished should be false when
* being flinged. We need to call computeScrollOffset() first so that
* isFinished() is correct.
*/
mScroller.computeScrollOffset();
mIsBeingDragged = !mScroller.isFinished();
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_TOUCH);
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
/* Release the drag */
mIsBeingDragged = false;
mActivePointerId = INVALID_POINTER;
recycleVelocityTracker();
if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0, getScrollRange())) {
ViewCompat.postInvalidateOnAnimation(this);
}
stopNestedScroll(ViewCompat.TYPE_TOUCH);
break;
case MotionEvent.ACTION_POINTER_UP:
onSecondaryPointerUp(ev);
break;
}
/*
* The only time we want to intercept motion events is if we are in the
* drag mode.
*/
return mIsBeingDragged;
}
正确的处理
即是重写RecyclerView,让上下滑动时不被拦截掉事件,让RecyclerView去处理事件。
public class NoConflictRecyclerView extends RecyclerView{
private float mStartX;
private float mStartY;
public NoConflictRecyclerView(Context context) {
super(context);
}
public NoConflictRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public NoConflictRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
mStartX = ev.getRawX();
mStartY = ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float endY = ev.getRawY();
float endX = ev.getRawX();
float x = endX - mStartX;
float y = endY - mStartY;
/* 左右滑动不拦截,上下滑动拦截*/
if (Math.abs(y) > Math.abs(x))
{
/* 已经在顶部了*/
if (y > 0 && !canScrollVertically(-1)){
getParent().requestDisallowInterceptTouchEvent(false);
}else if (y < 0 && !canScrollVertically(1)){
// 不能再上滑了 ========================
getParent().requestDisallowInterceptTouchEvent(false);
}else {
getParent().requestDisallowInterceptTouchEvent(true);
}
}else {
getParent().requestDisallowInterceptTouchEvent(false);
}
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public void onScrolled(int dx, int dy) {
super.onScrolled(dx, dy);
Log.e("Rv正在滑动","-dx ="+dx+"---dy ="+dy);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
}
}