AndroidPub
Published in

AndroidPub

📃RecyclerView SmoothScrollToPosition Not Working within NestedScrollView Solution— Android.✔

Scroll your recycler view within a Nested scroll view easily

Photo Credits

This Story belongs to what I have learned today. As you can see from the article title that recyclerView smoothScrollToPosition() not working within NestedScrollView, now how the layout file looks like and what I mean to say that all the confusion will be clear so you can see the XML file from the below Snippet.

Visit my website at https://danishamjad.com/

I had a case that I have a screen with a top static section and then I have a recycler view in which I need to show the comments list. And I need to scroll the whole screen that’s why I used NestedScrollView.

When the user sends a comment it will be displayed on the top of the List means on 0 positions, so in my case, I need to scroll the List to 0 positions whenever user comments.

The screen which I used is like the below image.

Layout View

After so many efforts I came to the solution, that recyclerView scroll not working whenever we add within the nested scroll view. That is why smoothScrollToPosition() not working.

Now, without wasting the time let’s jump to the solution 🛫

Solution:

The solution is to use nestedscroll.smoothScrollTo(0,0) here you need to use the nested scroll Id.

Now, this will let you move towards the top of the screen, but in my case, I need to scroll from where I started the recycler view, so for that, you need to use the view height and need to define in your Y position like below:

nestedScroll.smoothScrollTo(X,Y)
nestedScroll.smoothScrollTo(0,recycler.top)

Conclusion

This article described you to how RecyclerView SmoothScrollToPosition working within NestedScrollView.

I hope this article is helpful. If you think something is missing, have questions, or would like to give feedback, go ahead and leave a comment below. I’d appreciate the feedback.

I’ve written some other Android-related content, and if you liked what you read here, you’ll probably also enjoy this:

Sharing (knowledge) is caring 😊 Thanks for reading this article. Be sure to clap or recommend this article if you found it helpful. It means a lot to me.

If you need any help, join me on Twitter, LinkedIn, GitHub, and Subscribe to my Youtube Channel.

--

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Recommended from Medium

How memory is allocated and deallocated for classes and variables in android

Image Recognition / Classification Android App using TensorFlow model | Easy and fast…

Android Room+MVVM Part 2: Hilt, ClickEvent of RecyclerViewAdapter &. @ViewModelInject

3 Best Virtual pet Apps on your Android

3 Best Virtual pet Apps on your Android

How to solve adb mismatch version issue

Dirty Mouth 3D — Clean Ugly Teeth & Go On a Date!

Don’t Starve: Pocket Edition

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Danish Amjad

Danish Amjad

Senior Software Engineer(Android). Open Source Contributor, Technical Writer, Email: Dani.amjad12@gmail.com check my website at https://danishamjad.com/

More from Medium

Basics for your first Android App: REST API, Database, and Fragment Navigation

Some Best Practices for Android App Architecture

How to pass params on App Switch from Cordova to KOTLIN App

OpenGL ES: Render A 2D texture in Android App