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

Scroll to Specific Element #197

Open
logicgupta opened this issue Sep 12, 2020 · 2 comments
Open

Scroll to Specific Element #197

logicgupta opened this issue Sep 12, 2020 · 2 comments

Comments

@logicgupta
Copy link

java.lang.IllegalArgumentException: target position out of bounds: position=1, itemCount=0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3307)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7550)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.IllegalArgumentException: target position out of bounds: position=1, itemCount=0
at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.checkTargetPosition(DiscreteScrollLayoutManager.java:742)
at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.smoothScrollToPosition(DiscreteScrollLayoutManager.java:365)
at androidx.recyclerview.widget.RecyclerView.smoothScrollToPosition(RecyclerView.java:1790)

@janvalji
Copy link

janvalji commented Jan 2, 2021

same +1 @yarolegovich

@muhammad-farhan-bakht
Copy link

I have fixed this problem and sharing my solution here so if anyone who also faced this problem can work with it.

public void smoothScrollToUserSelectedPosition(DiscreteScrollView discreteScrollView, int imgIndex) {
        try {
            final RecyclerView.Adapter<?> adapter = discreteScrollView.getAdapter();
            int destination = imgIndex;
            if (adapter instanceof InfiniteScrollAdapter) {
                destination = ((InfiniteScrollAdapter<?>) adapter).getClosestPosition(destination);
            }
            discreteScrollView.smoothScrollToPosition(destination);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Call Like this:

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                smoothScrollToUserSelectedPosition(discreteScrollView, imgIndex);
            }
        }, 100);

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