Reputation: 155
I'm trying to show another text for every page of the ViewPager inside a TextView. But when the app is startet it isn't showing the text at all and when I change the page, only the default mode is shown.
I've put the following code inside of the onCreate
in the MainActivity
:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
final TextView myTextView = findViewById(R.id.my_textview);
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
myTextView.setText("TextView1");
case 1:
myTextView.setText("TextView2");
case 2:
myTextView.setText("TextView3");
default:
myTextView.setText("TextView");
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Upvotes: 0
Views: 47
Reputation: 341
You are not using break
in your switch statement cases which will cause all the cases below the valid one to execute including the default
Also the TextView will not show anything in the beginning because you added the listener after the view is created so you could use viewpager.getCurrentItem()
in your onCreate()
to show the correct text for the first time
Upvotes: 1
Reputation: 78
No text is being shown when your application starts because no page has been selected meaning the onPageSelected method is never being fired.
The default block is always executed when a page is selected because after each case you never use the break keyword. This means that when case 0, case 1 or case 2 happens it will fall through to the default case.
Upvotes: 0