当我在marshmallow设备上测试我的应用时,我的标签标题出现了一个奇怪的问题。当我在所有其他设备上测试时,标题显示得很好。在调试时,设备会点击所有相同的点,包括我的适配器中的“getPageTitle”方法。我已经阅读了很多关于使用android的TabLayout和ViewPager的教程,只是为了绝对肯定我没有遗漏任何东西,而且据我所知,我没有做错任何事。
以下是我的主要活动中的代码
// setup viewPager
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
setupViewPager(viewPager);
// setup tabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
我的setUpViewPager()方法,
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
viewPager.setAdapter(adapter);
}
和我的适配器类,它扩展了FragmentPagerAdapter
private class ViewPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_FRAGMENTS = 2;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return AllFeaturesFragment.newInstance();
case 1: return CategoriesFragment.newInstance();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "All Features";
case 1: return "Categories";
}
return null;
}
@Override
public int getCount() {
return NUM_FRAGMENTS;
}
}
非常感谢任何帮助!
修改
我从未想过检查xml文件的答案,但我在TabLayout元素上使用了一种样式,其父样式为“Widget.AppCompat.Toolbar”。出于某种原因,这种风格使得棉花糖设备上的标题看不见,而其他设备都看不到。我所要做的就是删除样式,它工作正常!
答案 0 :(得分:4)
我也面临同样的问题。问题是,当您对寻呼机适配器内的标签区块进行硬编码时,支持库中会有一些issue。我像这样改变了寻呼机适配器
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
然后你可以在这样的活动中添加片段
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
}
答案 1 :(得分:3)
替换
tabLayout.setupWithViewPager(viewPager);
与
viewPager.addOnPageChangeListener(
new TabLayout.TabLayoutOnPageChangeListener(tabLayout)
);