我使用TabLayout
和非刷卡ViewPager
創建了圖標底部的標籤。我想要的是打開一個Activity
點擊一個特定的標籤(這裏position == 2
)。其他選項卡將按照原樣打開Fragment
。特定選項卡上打開的活動單擊
有沒有辦法做到這一點?
public class MainActivity extends AppCompatActivity {
private TabLayout tabs;
private NonSwipeViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (TabLayout)findViewById(R.id.tabs);
viewpager = (NonSwipeViewPager)findViewById(R.id.viewpager);
viewpager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this));
viewpager.setPagingEnabled(false);
tabs.setupWithViewPager(viewpager);
setupTabIcons();
}
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new HomeFragment();
case 1:
return new CategoryFragment();
case 2:
return new AddProdFragment();
case 3:
return new MyProdFragment();
case 4:
return new MyProfileFragment();
default:
return null;
}
}
}
private void setupTabIcons() {
tabs.getTabAt(0).setIcon(R.drawable.home_white);
tabs.getTabAt(1).setIcon(R.drawable.category_white);
tabs.getTabAt(2).setIcon(R.drawable.upload_white);
tabs.getTabAt(3).setIcon(R.drawable.listing_white);
tabs.getTabAt(4).setIcon(R.drawable.user_white);
}
}
到的MenuItems相似? –
在mTabHost.getCurrentTab()中獲得標籤位置,然後根據需要加載片段。 –