loicteillard/EasyTabs

Adding another OnPageChangeListener is corrupted

Opened this issue · 2 comments

When we add another OnPageChangeListener to the ViewPager it is not correctly working when the first change occurs. This is caused by this code :

getViewPager().removeOnPageChangeListener(mOnPageChangeListener);
getViewPager().setCurrentItem(selected, true);
getViewPager().addOnPageChangeListener(mOnPageChangeListener);

Because we add our OnPageChangeListener as the second one, when the ViewPager method dispatchOnPageSelected(int position) is executed

for (int i = 0, z = mOnPageChangeListeners.size(); i < z; i++) {
    OnPageChangeListener listener = mOnPageChangeListeners.get(i);
        if (listener != null) {
            listener.onPageSelected(position);
        }
}

at first run easyTabs changeListener is notified, and then again easyTabs changeListener is notified, because we removed it, and added again. So our changeListener moved in the meantime from the second position in the list to the first one, and was skipped.

So I thought that I add my OnPageChangeListener first, but it still not working because you are doing getViewPager().clearOnPageChangeListeners();

I think that clearing should be removed and everything should be working fine. If not, why ?

I know that I can use setPagerListener but it adds to my code dependecy to your library, and I don't want it.

Yes i think you are right if i remove getViewPager().clearOnPageChangeListeners(); it solves your problem if you don't want to rely on the internal setPagerListener .
Thank you, i will make this change later, anyway i have several things to do for this library, it needs more work for the next step (onconfigurationchanged-aware, icons, androidx...).

I made some modifications of your library and you can find it on my profile as ModifiedEasyTabs. I tried to make a pull request, but something went wrong and I let it go. If you want you can copy my code.