Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid target position when scrolling #44

Closed
youfacepalm opened this issue Sep 4, 2017 · 7 comments
Closed

Invalid target position when scrolling #44

youfacepalm opened this issue Sep 4, 2017 · 7 comments

Comments

@youfacepalm
Copy link

Hi, Thanks for the great library. I encountered an app crash with the following stacktrace when using the latest (1.3.0) version. I simply fetch a list from an api and update the RV adapter with notifyDatasetChanged() after setting the new list. It works fine if there are more than 2 items in the list. But it crashes if there are 2 or fewer items in the list. The crash occurs soon after I scroll the first item.

java.lang.IllegalArgumentException: Invalid target position
    at android.support.v7.widget.RecyclerView$SmoothScroller.start(RecyclerView.java:11132)
    at android.support.v7.widget.RecyclerView$LayoutManager.startSmoothScroll(RecyclerView.java:7871)
    at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.startSmoothPendingScroll(DiscreteScrollLayoutManager.java:463)
    at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.onScrollEnd(DiscreteScrollLayoutManager.java:390)
    at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.onScrollStateChanged(DiscreteScrollLayoutManager.java:351)
    at android.support.v7.widget.RecyclerView.dispatchOnScrollStateChanged(RecyclerView.java:4746)
    at android.support.v7.widget.RecyclerView.setScrollState(RecyclerView.java:1434)
    at android.support.v7.widget.RecyclerView.cancelTouch(RecyclerView.java:3023)
    at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:3001)
    at android.view.View.dispatchTouchEvent(View.java:11721)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2929)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2636)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:445)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1828)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3292)
    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.DecorView.dispatchTouchEvent(DecorView.java:407)
    at android.view.View.dispatchPointerEvent(View.java:11960)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4776)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4590)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4274)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)
@youfacepalm
Copy link
Author

The currentPosition was -1 before the crash in DiscreteScrollLayoutManager startSmoothPendingScroll method

@yarolegovich
Copy link
Owner

Thanks for the report, unfortunately I won't be able to look into it until weekends.

@youfacepalm
Copy link
Author

Hey no problem at all. I traced down the issue and found out it is caused by how the DiscreteScrollView is placed in the layout. I have created a sample project that can reproduce the crash. I hope it saves you some time.

https://drive.google.com/open?id=0B4VSgmZteixRX002ZDVYR1c0RnM

Thanks again for all the effort!

@yarolegovich
Copy link
Owner

Yeah, the issue exists, I've checked the project. Can't yet say why it happens.
You started getting it after upgrading to 1.3?

@youfacepalm
Copy link
Author

It happened even before 1.3

@bearprada
Copy link

I found the root cause might come from DiscreteScrollLayoutManager.onScrollEnd()

        Direction scrollDirection = Direction.fromDelta(scrolled);
        if (Math.abs(scrolled) == scrollToChangeCurrent) {
            currentPosition += scrollDirection.applyTo(1); <- it will return -1 if the recyclerview has zero items
            scrolled = 0;
        }

@yarolegovich
Copy link
Owner

@youfacepalm thanks a lot for the project you created, it saved me a lot of time. The issue is resolved.

jjhesk pushed a commit to HKMOpen/DiscreteScrollView that referenced this issue Nov 12, 2017
Ensure that currentPosition is valid when onLayoutChildren is called and there items in the adapter.

closes yarolegovich#44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants