material-components / material-components/material-components-android
[TabLayoutMediator] populateTabsFromPagerAdapter() is not taking in account current selected item
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
**Description:** When an update is made in ViewPager adapter all tabs are recreated, but current item is not centered.
**Expected behavior:** Current selected tab must be in the center of TabLayout.
**Source code:**
void populateTabsFromPagerAdapter() {
tabLayout.removeAllTabs();
if (adapter != null) {
int adapterCount = adapter.getItemCount();
for (int i = 0; i < adapterCount; i++) {
TabLayout.Tab tab = tabLayout.newTab();
tabConfigurationStrategy.onConfigureTab(tab, i);
tabLayout.addTab(tab, false);
}
// Make sure we reflect the currently set ViewPager item
if (adapterCount > 0) {
int lastItem = tabLayout.getTabCount() - 1;
int currItem = Math.min(viewPager.getCurrentItem(), lastItem);
if (currItem != tabLayout.getSelectedTabPosition()) {
tabLayout.selectTab(tabLayout.getTabAt(currItem));
}else{
//TODO scroll TabLayout to selected item without animation
}
}
}
}
**Android API version:** 10
**Material Library version:** 1.2.1
**Device:** Xiaomi Mi A2
To help us triage faster, please check to make sure you are using the [latest version](https://github.com/material-components/material-components-android/releases) of the library.
We also happily accept [pull requests](https://github.com/material-components/material-components-android/pulls).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating TabLayoutMediator.populateTabsFromPagerAdapter() and reproduce an adapter update with a non-first selected ViewPager item. Check how the selected tab and TabLayout scrolling are handled after tabs are recreated. Done means the current selected tab is centered without an unintended animation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100