material-components / material-components/material-components-android
[TabLayoutMediator] populateTabsFromPagerAdapter() is called on every notify type in RecyclerView adapter
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 you instantiate TabLayoutMediator(... autoRefresh = true) you register PageDataObserver that invokes populateTabsFromPagerAdapter() on every type of notify(notifydataSetChange(),notifyItemChanged(int position)...) and every time the method will remove all the tabs and create a new ones and triger onConfigureChange(TabLayout.Tab tab, Int position) * (adapter.getItemCount()). Ask yourselves what if I notify only one item or maybe remove one item. Why do we need to recreate the whole TabLayout. What if our adapter list has 100 and more items?
**Expected behavior:**
I would expect if you call notifyItemChange(int position) that you call onConfigureChange to only that specific item or if you remove an item then remove that selected tab not recreate the whole TabLayout data.
**Source code:**
private class PagerAdapterObserver extends RecyclerView.AdapterDataObserver {
PagerAdapterObserver() {}
@Override
public void onChanged() {
populateTabsFromPagerAdapter();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
populateTabsFromPagerAdapter();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
populateTabsFromPagerAdapter();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
populateTabsFromPagerAdapter();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
populateTabsFromPagerAdapter();
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
populateTabsFromPagerAdapter(int positionStart, int itemCount);
}
}
}
**Proposed solution** I would suggest implementing case methods : updateTabFromPageAdapter(), updateTabsRangePageAdapter(int positionStart, int itemCount), removeTabsFromPageAdapter(int positionStart, int itemCount) etc... there will be no need for attaching and detaching the TabLayoutMediator when you call other types of notify in the adapter and also TabLayout scrollTo animation bug when TabLayout is
Scrollable not Fixed.
**Material Library version:** 1.2.0-alpha03
**Device:** On every device
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.
Assessment
This issue has not been assessed yet.