I want to create a recyclerview with nestedscrolling enabled inside every item. For that I have craeted custm recyclerview used this tutorial,
Here is the code,
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;
}
}
This works really well in lollipop and above version device but in kitkat app crash on code
super.onNestedScrollAccepted(child, target, axes);
Its gives error,
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
I'm not sure what's wrong with the code but every time i started scrolling inside recyclerview item it gives the error.
I have searched a lot on the internet but didn't find anything.
Please help me out!!!
onNestedScrollAccepted()
was added in API 21. You can't use it in lower API levels.