Wrong tab indicator in ViewPager / TabLayout support libraries when dynamically deleting tabs
OK, I was working with new Support Library
ViewPager
and TabLayout
, and in my application I dynamically add and remove tabs based on what the user SearchView
was open / closed.
My work execution; however, an indicator appears with a tab indicator. The correct slice of the tab is displayed, but the tab indicator is under the wrong tab.
I tested a lot to check if this was my problem, but then I thought it was a bug in the support library. I submitted a bug report to Google , but the status of the report was marked asuserError.
So a google developer gave me an idea of ββwhat to do. I tried the fix; however, nothing to fix. I've done some independent testing in the last few hours based on what he said the correct behavior depends on whether ViewPager
OnPageChangedListener
.
I added more information to my bug report, but since it was flagged as userError, I don't know if the developer will answer. That is why I am here now.
Screen recording
Here is the screen recording of the problem {NOTE: Clicking this link will automatically load the screen recording [. mp4] }. Pay attention to the indicator of the tab. It should go to the previous displayed tab, but it doesn't.
Questions
1. Correct indicator, incorrect displayed fragment
@Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
clearAllTabs();
addTabs(displayedTabs);
viewPager.setCurrentItem(previousTabPosition, true);
tabLayout.removeAllTabs();
tabLayout.setupWithViewPager(viewPager);
return true;
}
StackTrace:
First go to the second tab, click on SearchView, then close SearchView ....
On MenuItemCompat.OnActionExpandListener::onMenuItemActionCollapse(...)
clearAllTabs() [A, B, C, Search] -> []
addTabs(displayedTabs) [] -> [A, B, C]
ViewPager.OnPageChangeListener::OnPageSelected(2)
ViewPager.OnPageChangeListener::OnPageScrolled(2, 0.0, 0)
viewPager.setCurrentItem(1, true)
ViewPager.OnPageChangeListener::onPageScrollStateChanged(2)
ViewPager.OnPageChangeListener::onPageSelected(1)
tabLayout.removeAllTabs()
tabLayout.setupWithViewPager(viewPager);
ViewPager.OnPageChangeListener::onPageSelected(0) [NOTE: this statement does not fire if Tab A was displayed before opening SearchView]
return true
ViewPager.OnPageChangeListener::onPageScrolled(0, 0.0, 0)
// UI display is updated (Tab 1 fragment is displayed, but tab indicator is on 2nd Tab)
ViewPager.OnPageChangeListener::onPageScrollStateChanged(0)
2. Correct displayed snippet, incorrect tab indicator
@Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
clearAllTabs();
addTabs(displayedTabs);
tabLayout.removeAllTabs();
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(previousTabPosition, true);
return true;
}
StackTrace:
First go to the second tab, click on SearchView, then close SearchView ....
On MenuItemCompat.OnActionExpandListener::onMenuItemActionCollapse(...)
clearAllTabs() [A, B, C, Search] -> []
addTabs(displayedTabs) [] -> [A, B, C]
ViewPager.OnPageChangeListener::OnPageSelected(2)
ViewPager.OnPageChangeListener::OnPageScrolled(2, 0.0, 0)
tabLayout.removeAllTabs()
tabLayout.setupWithViewPager(viewPager);
ViewPager.OnPageChangeListener::onPageScrollStateChanged(2)
ViewPager.OnPageChangeListener::onPageSelected(0)
viewPager.setCurrentItem(1, true)
ViewPager.OnPageChangeListener::onPageSelected(1)
return true
ViewPager.OnPageChangeListener::onPageScrolled(1, 0.0, 0)
// UI display is updated (Tab 2 fragment is displayed, but tab indicator is on 3rd Tab)
ViewPager.OnPageChangeListener::onPageScrollStateChanged(0)
Github
Here is my GitHub repository
-
SearchView collapsing code (line 291)
source to share
No one has answered this question yet
Check out similar questions: