我有一个带片段的标签式活动。
选项卡的数量来自默认代码:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 4 total pages.
return 6;
}
}
我在我的标签上添加了图标,代码如下:
private int[] tabIcons = {
R.drawable.web,
R.drawable.social,
R.drawable.contact,
R.drawable.partners,
R.drawable.exclusivevideo,
R.drawable.jobifyblack
};
在onCreate(Bundle savedInstanceState)里面我使用的代码如下:
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(container);
mViewPager.setAdapter(mSectionsPagerAdapter);
//Add icons on tabs
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(4).setIcon(tabIcons[4]);
tabLayout.getTabAt(5).setIcon(tabIcons[5]);
默认情况下,所有标签都在AppBarLayout内生成。
现在我唯一需要的是如何在最后一个标签上触发点击以打开一个新活动,而不是在容器内打开片段。
我试了很多例子没有运气!