Ahmad-Hamwi / TabSync

An Android lightweight synchronizer between (Views) RecyclerView & TabLayout, (Compose) LazColumn and TabRow.
Apache License 2.0
154 stars 13 forks source link

NullPointerException in recyclerview scroll #1

Closed AbdulazizRasulbek closed 3 years ago

AbdulazizRasulbek commented 3 years ago

When I scroll recyclerview, TabbedListMediator class code from line 185 to 190 throws NullPointerException

if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == mIndices.size - 1) {
                            if (!mTabLayout.getTabAt(mIndices[mIndices.size - 1])!!.isSelected) {
                                mTabLayout.getTabAt(mIndices[mIndices.size - 1])!!.select()
                            }
                            return
                        }

mIndices[mIndices.size - 1] is equal to 48 which is index in getTabAt(index) method but Tablayout has only 5 tabs. So getTabAt(48) returns null

LayoutManager GridlayoutManager spanCount = 2

Ahmad-Hamwi commented 3 years ago

Hi Abdulaziz. Apparently, the error is providing the value that's corresponding to the index in the indices array to the TabLayout rather than the index itself. This works on the sample provided in the repository, but the one in your use case. Thanks for opening this issue, and sorry for the bug, I'll be providing a fix shortly.

AbdulazizRasulbek commented 3 years ago

Currently I downloaded the lib and changed not null(!!) to safe operator(?) and works fine. Could you explain the function of this code from line 185 to 190?

Ahmad-Hamwi commented 3 years ago

I've uploaded the change to this repository, could you use the latest changes?

This piece of code checks for whether you have two items fully visible in the RecyclerView, but only the second last tab will be selected (that's the behavior the library has implemented), but this piece of code checks one more time if you're in the last index and selects the last tab.

Your solution works with no null exceptions for now, and you won't be noticing any problems if your last two indexed items are far from each other and not visible at the same time, but you won't be able to see the last tab selected if both of them are fully visible at the bottom of your RecyclerView.

I'm sorry for not publishing the changes to maven yet, I'm REALLY having trouble with Gradle and pushing to Maven Central.

Please let me know if your problem is fixed using the code in the repo for now.

Ahmad-Hamwi commented 3 years ago

@AbdulazizRasulbek I've uploaded a new release to MavenCentral with a version of 1.0.1 Make sure you're using this version get your problem fixed.

implementation 'io.github.ahmad-hamwi:tabsync:1.0.1'

For any further problems, please let me know.

AbdulazizRasulbek commented 3 years ago

@Ahmad-Hamwi I copied code in TabbedListMediator class in repository and everything works fine. Thanks for the update