android NoSuchMethodError: android.support.v7.widget.RecyclerView.onNestedScrollAccepted

android NoSuchMethodError: android.support.v7.widget.RecyclerView.onNestedScrollAccepted

我想创建一个在每个项目中启用嵌套滚动的回收视图。为此,我创建了 custm recyclerview 使用本教程,

https://medium.com/widgetlabs-engineering/scrollable-nestedscrollviews-inside-recyclerview-ca65050d828a

这是代码,

public class NestedRecyclerView extends RecyclerView implements NestedScrollingParent {

private View nestedScrollTarget;
private boolean nestedScrollTargetIsBeingDragged = false;
private boolean nestedScrollTargetWasUnableToScroll = false;
private boolean skipsTouchInterception = false;

public NestedRecyclerView(Context context) {
    super(context);
}

public NestedRecyclerView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public NestedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    boolean temporarilySkipsInterception = nestedScrollTarget != null;
    if (temporarilySkipsInterception) {
        // If a descendent view is scrolling we set a flag to temporarily skip our onInterceptTouchEvent implementation
        skipsTouchInterception = true;
    }

    // First dispatch, potentially skipping our onInterceptTouchEvent
    boolean handled = super.dispatchTouchEvent(ev);

    if (temporarilySkipsInterception) {
        skipsTouchInterception = false;

        // If the first dispatch yielded no result or we noticed that the descendent view is unable to scroll in the
        // direction the user is scrolling, we dispatch once more but without skipping our onInterceptTouchEvent.
        // Note that RecyclerView automatically cancels active touches of all its descendents once it starts scrolling
        // so we don't have to do that.
        if (!handled || nestedScrollTargetWasUnableToScroll) {
            handled = super.dispatchTouchEvent(ev);
        }
    }

    return handled;
}

// Skips RecyclerView's onInterceptTouchEvent if requested


@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    return !skipsTouchInterception && super.onInterceptTouchEvent(e);
}

@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    if (target == nestedScrollTarget && !nestedScrollTargetIsBeingDragged) {
        if (dyConsumed != 0) {
            // The descendent was actually scrolled, so we won't bother it any longer.
            // It will receive all future events until it finished scrolling.
            nestedScrollTargetIsBeingDragged = true;
            nestedScrollTargetWasUnableToScroll = false;
        } else if (dyConsumed == 0 && dyUnconsumed != 0) {
            // The descendent tried scrolling in response to touch movements but was not able to do so.
            // We remember that in order to allow RecyclerView to take over scrolling.
            nestedScrollTargetWasUnableToScroll = true;
            target.getParent().requestDisallowInterceptTouchEvent(false);
        }
    }
}

@Override
public void onNestedScrollAccepted(View child, View target, int axes) {
    if (axes != 0 && View.SCROLL_AXIS_VERTICAL != 0) {
        // A descendent started scrolling, so we'll observe it.
        nestedScrollTarget = target;
        nestedScrollTargetIsBeingDragged = false;
        nestedScrollTargetWasUnableToScroll = false;
    }

    super.onNestedScrollAccepted(child, target, axes);
}

// We only support vertical scrolling.
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
    return (nestedScrollAxes != 0 && View.SCROLL_AXIS_VERTICAL != 0);
}

@Override
public void onStopNestedScroll(View child) {
    // The descendent finished scrolling. Clean up!
    nestedScrollTarget = null;
    nestedScrollTargetIsBeingDragged = false;
    nestedScrollTargetWasUnableToScroll = false;
}

}

这在 lollipop 及以上版本的设备中非常有效,但在 kitkat 应用程序中代码崩溃

super.onNestedScrollAccepted(child, target, axes);

它给出了错误,

java.lang.NoSuchMethodError: android.support.v7.widget.RecyclerView.onNestedScrollAccepted
    at com.ezadtv.ezmenu.views.custom.NestedRecyclerView.onNestedScrollAccepted(NestedRecyclerView.java:90)
    at android.support.v4.view.ViewParentCompat$ViewParentCompatBaseImpl.onNestedScrollAccepted(ViewParentCompat.java:49)
    at android.support.v4.view.ViewParentCompat.onNestedScrollAccepted(ViewParentCompat.java:377)
    at android.support.v4.view.NestedScrollingChildHelper.startNestedScroll(NestedScrollingChildHelper.java:154)
    at android.support.v4.widget.NestedScrollView.startNestedScroll(NestedScrollView.java:235)
    at android.support.v4.widget.NestedScrollView.onInterceptTouchEvent(NestedScrollView.java:726)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1859)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at com.ezadtv.ezmenu.views.custom.NestedRecyclerView.dispatchTouchEvent(NestedRecyclerView.java:39)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
    at android.view.View.dispatchPointerEvent(View.java:7886)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
    at android.view.ViewRootImpl.deliverInput

我不确定代码有什么问题,但每次我开始在 recyclerview 项目中滚动时都会出现错误。

我在互联网上搜索了很多,但没有找到任何东西。

请帮帮我!!!

onNestedScrollAccepted() 已添加到 API 21。您不能在较低的 API 级别使用它。