I have a tab layout in my application. I am adding tabs dynamically. Two tabs are added initially and it works fine then other six tabs are added after sometime in response to Network call. the problem is when six tabs are added in response of network call the whole tab layout scrolls automatically till end.
Few things to be noted:
This only happens in RTL layout, on LTR layout it's working fine.
If i set adapter for viewPager after getting all tabs in network call response then it's working fine.
Here is the code.
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setSmoothScrollingEnabled(true);
pagerAdapter = new PagerAdapter(getChildFragmentManager(), getContext(), tabs);
viewPager.setAdapter(pagerAdapter);
if(savedInstanceState == null) {
pagerAdapter.add(getTab1());
pagerAdapter.add(getTab2());
setCustomViews(getContext());
loadMoreTabs(); //Network call
}
private void loadMoreTabs(){
//in response of network call
onResponse(){
pagerAdapter.addAll(new ArrayList<Object>(response.body()));
setCustomViews(); //Here whole tab layout scrolls till end
}
I found why this happened. Actually the problem was in setting adapter first and adding items later in adapter. When adapter is already set and we try to add some item in adapter and call notifyDataSetChanged() then it will scroll to end [don't know the reason although]. Solution is setting adapter again instead of adding items in adapter. I know this is not a good approach but it works.
private void loadMoreTabs(){
onResponse(){
tabs.addAll(new ArrayList<Object>(response.body()));
viewPager.setAdapter(pagerAdapter);
setCustomViews();
}