I have sample project with TabLayout
and PagerAdapter
.
Strange things happens with TabLayout when I call pagerAdapter.notifyDataSetChanged();
after tabLayout.setupWithViewPager(viewPager);
TabLayout is scrolling to unknown x position so the current tab is not visible. However if I scroll to left to expecting tab, this tab has indicator.
What is going on? Could anyone help me? I have spent on it too many time.
Below the code.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
final SampleFragmentPagerAdapter pagerAdapter = new SampleFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pagerAdapter.notifyDataSetChanged();
}
});
}
}
I tested on nexus emulators and nexus real devices (api 21+)
Gradle settings:
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "xx.xxx.myapplication4"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Link to reported issue and ready to test project as attachment here
here
- the last paragraph of my post – Kurtiskurtosis- (getWidth() / 2)
it would work as expected. You can test that placing a breakpoint before returning from method and changingscrollBase
value to a correct one. See this. – Forzando-width
had some reasoning, which means you'll break something else by changing it. I guess there should be some if-else statement. – Forzando