我的ViewPager中的最后一个标签的标题未显示,in this case i have 4 tabs but only the first 3 tabs are shown but not the last one although I can swipe to the fourth tab
代码
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import java.util.Objects;
import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;
public class MainActivity extends AppCompatActivity implements
MaterialTabListener {
MaterialTabHost tabHost;
ViewPager viewPager;
ViewPagerAdapter androidAdapter;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = this.findViewById(R.id.toolBar);
tabHost = this.findViewById(R.id.tabHost);
viewPager = this.findViewById(R.id.viewPager);
androidAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(androidAdapter);
viewPager.setOnPageChangeListener(new
ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int tabposition) {
tabHost.setSelectedNavigationItem(tabposition);
}
});
for (int i = 1; i < androidAdapter.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setText(Objects.requireNonNull(androidAdapter.getPageTitle(i)))
.setTabListener(this)
);
}
}
@Override
public void onTabSelected(MaterialTab materialTab) {
viewPager.setCurrentItem(materialTab.getPosition());
}
//tab on reselected
@Override
public void onTabReselected(MaterialTab materialTab) {
}
//tab on unselected
@Override
public void onTabUnselected(MaterialTab materialTab) {
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
public Fragment getItem(int num) {
return new AndroidFragment();
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int tabposition) {
return "Tab " + tabposition;
}
}}
我的ViewPager中最后一个选项卡的标题没有显示,在这种情况下,我有4个选项卡,但是尽管我可以滑动到第四个选项卡,但仅显示了前三个选项卡
答案 0 :(得分:0)
更改此行:
for (int i = 1; i < androidAdapter.getCount(); i++)
到
for (int i = 0; i < androidAdapter.getCount(); i++)
您缺少1个标签